![]() ![]() | ||
When you take a look at the code for this form, Form1 in our application (which you'll find in its code designer), the first thing you'll see is that Form1 is a public class, and that it inherits its functionality (inheritance was discussed in Chapter 1) from System.Windows.Forms.Form—that is, the Form1 class is derived from the Form class:
Public Class Form1 Inherits System.Windows.Forms.Form #Region " Windows Form Designer generated code " Public Sub New() MyBase.New() ⋮
Next is the Windows form designer code added by Visual Basic (which, as discussed in Chapter 1, you expand and collapse with the buttons at left in the code designer). The first thing you find in this code is the class's New method. This method is the first one run when you create an object from a class (as Visual Basic does automatically when you run this application). It is the form's constructor (constructors were mentioned in Chapter 2), which is a special method of a class automatically run when you create an object from the class and which is used to customize that object. Constructors can take arguments or not—in this case, the New method does not take any arguments. In the New Sub procedure, the code calls a procedure named InitializeComponent, which adds and arranges the controls in the form:
Public Class Form1 Inherits System.Windows.Forms.Form #Region " Windows Form Designer generated code " Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call End Sub '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 Friend WithEvents TextBox1 As System.Windows.Forms.TextBox Friend WithEvents Button1 As System.Windows.Forms.Button ⋮
Note that at the end of the above code, the actual text box, TextBox1, and button, Button1, are declared as objects of the System.Windows.Forms.TextBox and System.Windows.Forms.Button classes. As we'll see when we discuss object-oriented programming (OOP) in more detail, declaring them as Friend gives those objects access to the code in the form, and the WithEvents keyword enables event handling. Now that we have objects corresponding to the button and the text box, the code can initialize and position them in the InitializeComponent method; note that our event handler for button clicks appears at the very end of the code:
Public Class Form1 Inherits System.Windows.Forms.Form #Region " Windows Form Designer generated code " Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call End Sub '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 Friend WithEvents TextBox1 As System.Windows.Forms.TextBox Friend WithEvents Button1 As System.Windows.Forms.Button 'Required by the Windows Form Designer Private components As System.ComponentModel.Container '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.TextBox1 = New System.Windows.Forms.TextBox() Me.Button1 = New System.Windows.Forms.Button() Me.SuspendLayout() ' 'TextBox1 ' Me.TextBox1.Location = New System.Drawing.Point(32, 128) Me.TextBox1.Name = "TextBox1" Me.TextBox1.Size = New System.Drawing.Size(224, 20) Me.TextBox1.TabIndex = 0 Me.TextBox1.Text = "" ' 'Button1 ' Me.Button1.Location = New System.Drawing.Point(112, 56) Me.Button1.Name = "Button1" Me.Button1.TabIndex = 1 Me.Button1.Text = "Click Me" ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 213) Me.Controls.AddRange(New System.Windows.Forms.Control() _ {Me.Button1, Me.TextBox1}) Me.Name = "Form1" Me.Text = "Form1" Me.ResumeLayout(False) End Sub #End Region Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As _ System.EventArgs) Handles Button1.Click TextBox1.Text = "Welcome to Visual Basic" End Sub End Class
Tip |
There's a lot of code here for such a simple application—far more than you would have seen in VB6. However, the automatically generated code is restricted to a region that's collapsed by default in the code designer, so it's out of the way. And you have access to that code, which is what Visual Basic uses to set up the form. In VB6 and before, all the details of the form were stored in .frm files as data, but now you have direct access to the code that sets your form up. Microsoft recommends that you don't edit that code directly, but, of course, you can—and when you know what you're doing, you've gained a lot of power as compared with the VB6 days. Here's another tip: because the placement of controls is now set in code, you can duplicate projects easily just by copying all the code in one project and overwriting all the code in another—not only will the new code appear in the second project, but all the controls also will appear. |
You also might note the Me keyword in the above code; you use that keyword to refer to the current object, which in this case is the current form. For example, to set the Name property for a form, you can execute code like this:
Me.Name = "Form1"
Actually, you don't need the Me keyword here, because the properties of the current object are used by default in the code for that object, so this code will perform the same task:
Name = "Form1"
The Visual Basic code above that uses Me does so to make it explicit that it's referring to the current form. (Normally, you only use Me when you need some way of indicating the current form, as when you want to pass the current form to a procedure.)
That completes our survey of the code of this Windows application—we'll become more familiar with the structure of applications such as this in time, but this gives us enough of an overview to start. We've seen forms at work, and we've seen them in code. Now it's time to start handling detailed issues in the Immediate Solutions section.
![]() ![]() | ||