Datasheet
44
❘
CHAPTER 1 VISUAL STUDIO 2010
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 fi rst 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:
Me.SuspendLayout()
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(104, 23)
Me. ButtonTest.TabIndex = 0
Me. ButtonTest.Text = "Run Code"
Me. ButtonTest.UseVisualStyleBackColor = True
Code snippet from Form1.Designer
The Size property defi nes 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:
Me.Controls.Add(Me.ButtonTest)
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 fi nal code change. The form is given permission to
resume the layout logic:
Me.ResumeLayout(False)
In addition to the code that has been generated in the Form1.Designer.vb source fi le, you have created
code that lives in the Form1.vb source fi le:
CH001.indd 44CH001.indd 44 4/5/10 11:56:56 AM4/5/10 11:56:56 AM