VB.NET 1.1 Tutorial - Empty Visual Studio Winform ApplicationAlthough so far I have written everything by hand, I have also used Visual Studio to create each application afterwards. Before I go any further I thought it would be best to look at the code generated by Visual Studio, to see what I have been missing in my applications. Below is the code listing from Hello Mum - Windows Style - Part 1 when written using Visual Studio. 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
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'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()
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Name = "Form1"
Me.Text = "Hello Mum"
End Sub
#End Region
End Class
So What Extras Are There?#Region and #End Region are used by the Visual Studio editor to minimise chunks of code within the editing window. The New() method calls a second method, InitializeComponent(), to change the Text property of the form. In our initial app, we changed the Text property of the form in the New() method. The InitializeComponent() method also sets up a couple of other properties which we really should set up in our projects ourselves. The components variable is a instance of the Container class. Containers are objects that encapsulate and track zero or more components. For more info, see references below. DisposingFinally, the code includes a Dispose() method. A Dispose method should release all the resources that it owns. It should also release all resources owned by its base types by calling its parent type's Dispose method. For more info, see references below. There are several new keywords used in the Dispose method. The first is the Overloads keyword, this declares the method has same name as an existing method, but has an argument list different from the original method. The second is the Overrides keyword, this means that the method overrides a method inherited from a base class. Thirdly, the Boolean keyword, is a reference to a data type. A Boolean can only store a True or False value. If...Then...ElseThe main concept of If...Then is best explained on its own page. The Not operator performs a logical negation on a Boolean expression. The Is operator compares two references to object variables. The Nothing keyword represents the default value of any data type. StandardisingFrom now on, all of the examples will use a slight variant of the code above as the template for all winform applications. We use a variant because there is no Main method in the code above. ReferencesFor more information on the System.ComponentModel.Container class, visit MSDN at microsoft here. For more information on the Dispose, visit MSDN at microsoft here. For more information on the Overloads keyword, visit MSDN at microsoft here. For more information on the Overrides keyword, visit MSDN at microsoft here. For more information on Data Types, visit MSDN at microsoft here. For more information on If...Then...Else Statements, visit MSDN at microsoft here. For more information on the Not operator, visit MSDN at microsoft here. For more information on the Is operator, visit MSDN at microsoft here. For more information on the Nothing keyword, visit MSDN at microsoft here. What Next?Next we will create a simple event driven program. Return to the Tutorial Contents. |