Reviewing the Code

Now that you have created a simple Windows application, let's review the elements of the code that have been added by the IDE. Following is the entire Form1.Designer.vb source listing. Highlighted in this listing are the lines of code that have changed since the original template was used to generate this project:

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _ Partial Class Form1

Inherits System.Windows.Forms.Form

'Form overrides dispose to clean up the component list. <System.Diagnostics.DebuggerNonUserCode()> _ Protected Overrides Sub Dispose(ByVal disposing As Boolean) Try

If disposing AndAlso components IsNot Nothing Then components.Dispose() End If Finally

MyBase.Dispose(disposing) End Try End Sub

'Required by the Windows Form Designer

Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. <System.Diagnostics.DebuggerStepThrough()> _ Private Sub InitializeComponent()

Me.ButtonTest = New System.Windows.Forms.Button() Me.SuspendLayout()


Me.ButtonTest.Location = New System.Drawing.Point(13, 13) Me.ButtonTest.Name = "ButtonTest"

Me.ButtonTest.Size = New System.Drawing.Size(104, 23) Me.ButtonTest.TabIndex = 0 Me.ButtonTest.Text = "Run Code" Me.ButtonTest.UseVisualStyleBackColor = True


Me.AutoScaleDimensions = New System.Drawing.SizeF(8.0!, 16.0!)

Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font

Me.ClientSize = New System.Drawing.Size(32 8, 258)


Me.Name = "Form1"

Me.Text = "Professional VB.NET"


End Sub

Friend WithEvents ButtonTest As System.Windows.Forms.Button

End Class

Code snippet from Forml.Designer

After the class declaration in the generated file, the first change made to the code is the addition of a new variable to represent the new button:

Friend WithEvents ButtonTest As System.Windows.Forms.Button

When any type of control is added to the form, a new variable is added to the form class. Controls are represented by variables; and, just as form properties are set in code, form controls are added in code. The Button class in the System.Windows.Forms namespace implements the Button control on the Toolbox. Each control added to a form has a class that implements the functionality of the control. For the standard controls, these classes are usually found in the System.Windows.Forms namespace. The WithEvents keyword has been used in the declaration of the new variable so that it can respond to events raised by the button.

The bulk of the code changes are in the InitializeComponent procedure. Nine lines of code have been added to help set up and display the Button control. The first addition to the procedure is a line that creates a new instance of the Button class and assigns it to the button variable:

Me. ButtonTest = New System.Windows.Forms.Button()

Before a button is added to the form, the form's layout engine must be paused. This is done using the next line of code:


The next four lines of code set the properties of the button. The Location property of the Button class sets the location of the top-left corner of the button within the form:

Me. ButtonTest.Location = New System.Drawing.Point(13, 13)

The location of a control is expressed in terms of a Point structure. Next, the Name property of the button is set:

Me. ButtonTest.Name = "ButtonTest"

The Name property acts exactly as it did for the form, setting the textual name of the button. The Name property has no effect on how the button is displayed on the form; it is used to recognize the button's context within the source code. The next four lines of code assign values to the Size, TabIndex, Text, and UseVisualStyleBackColor properties of the button:

Me.ButtonTest.Size = New System.Drawing.Size(10 4, 23)

Me. ButtonTest.TabIndex = 0

Me. ButtonTest.Text = "Run Code"

Me. ButtonTest.UseVisualStyleBackColor = True

Code snippet from Forml.Designer

The Size property defines the height and width of the control; it is being set because the default button size didn't display the full label, and so the button's size was increased. The TabIndex property of the button is used to set the order in which the control is selected when a user cycles through the controls on the form using the Tab key. The higher the number, the later the control gains focus. Each control should have a unique number for its TabIndex property. The Text property of a button sets the text that appears on the button. Finally, the UseVisualStyleBackColor property indicates that when this button is drawn, it uses the current visual style. This is a Boolean value and typically you can accept this default, but you can customize the background so that a given button doesn't default to the current visual style.

Once the properties of the button have been set, it needs to be added to the form. This is accomplished with the next line of code:


The System.Windows.Forms.Form class (from which your Form1 class is derived) has a property called Controls that keeps track of all of the child controls of the form. Whenever you add a control to a form in the designer, a line similar to the preceding one is added automatically to the form's initialization process.

Finally, near the bottom of the initialization logic is the final code change. The form is given permission to resume the layout logic:


In addition to the code that has been generated in the Form1.Designer.vb source file, you have created code that lives in the Form1.vb source file:

Public Class Form1

Private m_count As Integer Public Property Count() As Integer Get

Return m_count End Get

Set(ByVal value As Integer)

m_count = value End Set End Property

''' <param name="sender"></param> ''' <param name="e"></param> ''' <remarks></remarks>

Private Sub ButtonTest_Click(ByVal sender As System.Object,

ByVal e As System.EventArgs) Handles ButtonTest.Click

MessageBox.Show("Hello World shown " + Count.ToString() + " times.", "Hello World Message Box", MessageBoxButtons.OK, MessageBoxIcon.Information)

End Sub End Class

Code snippet from Forml

This code reflects the event handler added for the button. The code contained in the handler was already covered, with the exception of the naming convention for event handlers. Event handlers have a naming convention similar to that in previous versions of Visual Basic: The control name is followed by an underscore and then the event name. The event itself may also have a standard set of parameters. At this point, you can test the application, but to do so let's first look at your build options.

Was this article helpful?

0 0

Post a comment