Form Properties Set in Code

As noted earlier, Visual Studio keeps every object's custom property values in the source code. To do this, it adds a method to your form class called InitializeComponent. As the name suggests, this method handles the initialization of the components contained on the form. A comment before the procedure warns you that the Form Designer modifies the code contained in the procedure, and that you should not modify the code directly. This module is part of the Form1.Designer.vb source file, and Visual Studio updates this section as changes are made through the IDE.

'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.SuspendLayout()


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

Code snippet from Forml.Designer

The seven lines of the InitializeComponent procedure assign values to the properties of your Form1 class. All the properties of the form and controls are now set directly in code. When you change the value of a property of the form or a control through the Properties window, an entry is added to InitializeComponent that assigns that value to the property. Previously, while examining the Properties window, you set the Text property of the form to Professional VB.NET, which caused the following line of code to be added automatically:

Me.Text = "Professional VB.NET"

The properties of the form class that are set in InitializeComponent are shown in Table 1-5.

TABLE 1-5: Properties Set by InitializeComponent property description

SuspendLayout Specifies that the form should not make updates to what is displayed to the user. It is called so that as each change is made, the form doesn't seem to appear in pieces

AutoScaleDimensions Initializes the size of the font used to lay out the form at design time . At runtime, the font that is actually rendered is compared with this property, and the form is scaled accordingly.

AutoScaleMode Indicates that the form will use fonts that are automatically scaled based on the display characteristics of the runtime environment .

ClientSize Sets the area in which controls can be placed (the client area) . It is the size of the form minus the size of the title bar and form borders.

Name This property is used to set the textual name of the form.

ResumeLayout This tells the form that it should resume the normal layout and displaying of its contents.

0 0

Post a comment