When you create a Windows Application project, it provides starter code for the main form and sets up references to the required .NET libraries. The Windows Forms Designer interface can then be used to design the forms. Code Generated by Windows Form Designer As you can see from Figure 7-18, the Code window hides the code that was generated by the Forms Designer. It uses a Region directive to place a + (plus) next to a code segment. When collapsed , the Region directive displays a comment. When the + is clicked, the Code window expands the code. Figure 7-18. The Code window hides generated code. The code generated by the Forms Designer defines a class that was derived from System.Windows.Forms.Form . Public Class MainWindow Inherits System.Windows.Forms.Form The constructor calls InitializeComponent to build and initialize all controls that are on the form. You can then provide initialization for any variables . Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the 'InitializeComponent() call End Sub The class also defines the Dispose method, which is used to clean up the form's contents before the form is destroyed . We discuss the Dispose design pattern and garbage collection in Chapter 10. 'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal _ disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub It defines variables that reference the control objects that were placed on the form: Friend WithEvents lblName As System.Windows.Forms.Label Friend WithEvents btnClickMe As System.Windows.Forms.Button Friend WithEvents txtName As System.Windows.Forms.TextBox Finally, we see the InitializeComponent method that was called in the constructor. It instantiates each control and initializes each property that has a non-default value. It also adds each control to the form's Controls collection via the AddRange method. By calling the form's SuspendLayout before the properties are set, and calling ResumeLayout at the end, the form waits until all controls have been specified before triggering any Layout events. As the comment states, the code in this method should not be modified because it is updated by the Form Designer. '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.lblName = New System.Windows.Forms.Label() Me.txtName = New System.Windows.Forms.TextBox() Me.btnClickMe = New System.Windows.Forms.Button() Me.SuspendLayout() ' 'lblName ' Me.lblName.Location = New System.Drawing.Point(8, 24) Me.lblName.Name = "lblName" Me.lblName.Size = New System.Drawing.Size(64, 20) Me.lblName.TabIndex = 0 Me.lblName.Text = "Your Name:" ' 'txtName ' Me.txtName.Location = New System.Drawing.Point(80, 24) Me.txtName.Name = "txtName" Me.txtName.Size = New System.Drawing.Size(192, 20) Me.txtName.TabIndex = 1 Me.txtName.Text = "" ' 'btnClickMe ' Me.btnClickMe.Location = New System.Drawing.Point(8, 72) Me.btnClickMe.Name = "btnClickMe" Me.btnClickMe.Size = New System.Drawing.Size(264, 40) Me.btnClickMe.TabIndex = 2 Me.btnClickMe.Text = "Click Me" ' 'MainWindow ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(280, 141) Me.Controls.AddRange _ (New System.Windows.Forms.Control() _ {Me.txtName, Me.btnClickMe, Me.lblName}) Me.Name = "MainWindow" Me.Text = "Second Program" Me.ResumeLayout(False) End Sub The class definition terminates with any event handlers and other code you wrote. As you can see, using Visual Studio .NET to generate a Windows application is much easier! |