The Code Behind the Scenes

You have created a functioning Windows application. Form1 has the default behavior of a window. For example, you can move Form1 by clicking its title bar and dragging it. You also can resize Form1 by holding your cursor over a border until it becomes a two-headed arrow and then clicking and dragging the border.

Additi onally, you created a functioning Windows application without having to write a single line of code. This is impressive since a substantial amount of complex code is required to create a window.

While you did not have to write a single line of code, there is code automatically generated by Visual Studio.NET. You can view the code created by Visual Studio.NET by the menu command intuitively named View | Code, after which you will see the following code:

Public Class Form1

Inherits System.Windows.Forms.Form

Region " Windows Form Designer generated code "

End Class

Figure 2-16 shows how this code window appears in the IDE.

Fublic Clui r:rin]

[rthicua 37ST.eir. tfi^ds-irs. Tiirins. Font ll ymfe.» r,t„

End Class

Figure 2-16: The Code window in IDE

The plus (+) symbol means that a section is collapsed, similar to collapsing the contents of a drive or folder in Windows Explorer. Normally, you do not need to change this code. In fact, since Visual Studio.NET automatically generates and maintains this code, changing it without knowing exactly what you are doing can result in unpredictable and unwanted consequences. However, analyzing the code is a good learning tool, and we will be careful in changing the code generated by Visual Basic .NET.

Click the plus (+) symbol next to #Region Windows Form Designer Generated Code. The following listing shows the code as expanded:

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. InitializeComponentQ

'Add any initialization after the InitializeComponentQ 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

MyBase.Dispose(disposing) End Sub

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

Private Sub InitializeComponentQ

'Form1

Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)

Me.ClientSize = New System.Drawing.Size(292, 273)

Me.Name = "Form1"

Me.Text = "Form1"

End Sub

#End Region

End Class

While this code may appear complex, with the foundation in object-oriented programming you obtained from Module 1, much of it is understandable. As the following analysis shows, the code is structured around the lifecycle of an object, from birth to death.

Class and Inherits Statements

The code starts with the statements:

Public Class Form1

Inherits System.Windows.Forms.Form

As discussed in Module 1 , a form is an object representing a window that makes up an application's user interface. Like all objects, a form is created from a template or blueprint called a class. The .NET Framework has a built-in Form class, System.Windows.Forms.Form. This class implements the characteristics and behavior that you would expect of a Windows form.

The Form class is located in a namespace called System.Windows.Forms. A namespace is a grouping of related classes in a .NET system class library. The full class name System.Windows.Forms.Form results from adding the class name Form to the namespace System.Windows. Forms, separating the two by a dot (.).

Each object is an instance of a class. Since you can and often do have more than one form in your application, you need to distinguish one form from another. You do so by giving each object a unique name. Visual Studio.NET has given our form the highly original name Form1. You can change this name, and you should if your application uses more than one form.

The Inherits statement indicates that Form1 will start with all the built-in characteristics and behavior of the Form class. Form1 has the characteristics of a standard window, such as a title bar, a border, and a client area. Form1 also has the behavior of a standard window. You can move Form1 by clicking its title bar and dragging it, and can resize Form1 by holding your cursor over a border until it becomes a two-headed arrow and then clicking and dragging the border.

New Method

As discussed in Module 1 , a class is only a blueprint or template for an object. It is not an object itself. Thus, the System.Windows.Forms.Form class is a template for a form, but it is not a form. Since a Windows application does require, after all, a window, you need to create, or instantiate, a form from the Form class. Like most aspects of programming, this requires code.

The code that creates an object from a class is called a constructor. In Visual Basic .NET, a constructor is always contained in a method that is called New, perhaps because the method creates a new object from the class.

Visual Studio.NET generates the following New method:

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 code in the New method is executed when the object is instantiated. The first step is to call the New method of the class on which the object is based, in this case the Form class. The MyBase keyword refers to the base (also called parent) class, which is the one specified by the Inherits statement discussed previously. The New method next calls the InitializeComponent method, which is discussed next.

Two of the lines of code start with a single-quote or apostrophe character ('): 'This call is required by the Windows Form Designer.

'Add any initialization after the InitializeComponent() call

A line of code that begins with a single-quote character is called a comment A comment is not considered part of the code of the application. The compiler ignores comments. Instead, the purpose of a comment is to explain the code.

Tip You should develop the habit of annotating your code with comments. You will find that you have trouble remembering your reasons for code you wrote only a week or two before. Comments will help you remember. Additionally, when you program for a living, often you will be part of a team, and comments help other members of the team understand your code.

InitializeComponent Method

The InitializeComponent method assigns values to various properties of the form. As discussed in Module 1 , a property is an attribute of an object. Each property has a value. For example, a Form object has a BackColor property that represents the form's background color. The actual background color of the form is the value of the BackColor property.

If you do not provide a value for the property, the value is the default—that is, the value given by Visual Studio.NET in the absence of direction from the programmer. For example, the default value of the BackColor property, and thus the background color of the form, is gray. However, the form's background color could be blue, red, or some other color.

The process of giving a property a value is known as assignment, as in assigning a value to a property. Assignment is performed by the assignment operator, which looks like an equal sign (=). Several assignments of values to properties are performed in the code for the InitializeComponent method, which stripped of comments reads:

Private Sub InitializeComponent()

Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 273) Me.Name = "Form1" Me.Text = "Form1" End Sub

Table 2-3 describes these properties.

Table 2-3: Form Properties Used in the InitializeComponent Method

Property

Description

AutoScaleBaseSize

A complex property, it relates to how a form adjusts its size and the size of the controls it contains in relation to the height of the font used on the form.

ClientSize

The size of the form's client area. In Microsoft Word, the client area is the area in which you can type, and therefore does not include the menus, toolbars, and so on.

Name

The form's identifier. Discussed in the following text.

Text

The words displayed in the form's title bar. Discussed in the following text.

The Ask the Expert" following this section explains how you can obtain a listing and description of the properties of a Form object.

Let's analyze one of the statements: Me.Text = "Form1"

The Me keyword refers to the specific instance of the class where the code is executing. In this case, it refers to Form1, a specific instance of the Form class. Text is a property of the Form object. The Text property is the text displayed in the title bar of the Form. Me.Text means that the reference is to the Text property of the instance of the class currently executing, here Form1. The object instance (Me) and the object property (Text) are separated by a dot consistent with the syntax:

[Name of Object Instance].[Name of Property]

The object instance and property are followed by the assignment operator and the value to be assigned to the property of that object instance. Thus, the entire syntax of the line is:

[Name of Object Instance].[Name of Property]= New Value of Property

The effect is to give the Text property of Form1 the value on the right side of the assignment operator, "Form1". The value "Form1" on the right side of the assignment operator is the text displayed in the title bar of the form.

The value on the right side of the assignment operator, "Form1", is surrounded by double quotes. This indicates that it is literal text, referred to in Visual Basic .NET as a string. By contrast, when the referenceis to the object Form1, as in:

Public Class Form1

The reference stands by itself and is not surrounded by double quotes.

The effect of an assignment of a value to a property is illustrated by changing the code: Me.Text = "Form1"

Me.Text = "Godzilla"

Run the project with the Debug | Run menu command. Figure 2-17 shows the result. The text displayed by the title bar now reads "Godzilla."

Was this article helpful?

0 0

Post a comment