How do I make my first “Hello, WinForm” .NET form application without VS.NET in Visual Basic.NET?
VB.NET Version
Building the "Hello WinForm" Application
Let's build our first windows application, which we will call "Hello WinForm". The application will present a simple window with a "Hello WinForm" greeting at the center. The source code of the program is:
Imports System
Imports System.Windows.Forms
Imports System.Drawing
Class Test
Public Sub Main()
Application.Run(New MyForm())
End Sub
End Class
Class MyForm
Inherits Form
Public Sub New()
MyBase.new()
Me.Text = "My First Windows Application"
Me.Size = New Size(300, 300)
Dim lblGreeting As New Label()
lblGreeting.Text = "Hello WinForm"
lblGreeting.Location = New Point(100, 100)
Me.Controls.Add(lblGreeting)
End Sub
End Class
Understanding the Code
At the start, we included three namespaces in our application:
Imports System
Imports System.Windows.Forms
Imports System.Drawing
The System namespace, as we stated in the first lesson, is the necessary ingredient of all VB.NET applications. In fact, the Application class that we used later in the Main() method is defined in this namespace. The System.Windows.Forms namespaces contains the base classes for windows controls, e.g. Form, Label and Button. Finally, including the System.Drawing namespace is necessary as it contains the classes related to the drawing of controls. The Size and Point classes used later in the program are actually defined in the System.Drawing namespace.
The System.Windows.Forms and System.Drawing namespaces can not be added to a project just by writing Imports statement for them. We also need to add references to the assemblies (for now take assemblies as Windows DLL’s or library files for .NET) that contain the code of types these namespaces. For this we need to add reference to System.Windows.Forms.dll and System.Drawing.dll. To add a reference write click the ‘Reference’ folder under the current project in the solution explorer and select Add Reference… It will show the following window
From the '.NET' tab of the Add Reference Dialog box, select the System.Windows.Forms.dll and System.Drawing.dll and select OK. This completes the process of adding references to assemblies in your project. Lets go back to our sample application.
Later, we derived a new class, 'MyForm', from the Form class defined in
System.Windows.Forms.
Class MyForm
Inherits Form
...
End Sub
End Class
In the constructor of MyForm, we specified the size and title of the form (by setting the size and text properties). The size is defined using the System.Drawing namespace's Size class. We passed two integers to the constructor of Size to specify the width and the height of the form.
Public Sub New()
MyBase.new()
Me.Text = "My First Windows Application"
Me.Size = New Size(300, 300)
Next in the constructor, we created a text label and added it to the Controls collection of the Form. A text label is used to write some text on the form. The System.Windows.Forms.Label class defines a text label in a Windows application. We set the text of the Label using its Text property, which is of the string type. All the controls contained by a form must be added to its Controls collection; hence we have also added our label to this collection.
Public Sub New()
MyBase.new()
Me.Text = "My First Windows Application"
Me.Size = New Size(300, 300)
Dim lblGreeting As New Label()
lblGreeting.Text = "Hello WinForm"
lblGreeting.Location = New Point(100, 100)
Me.Controls.Add(lblGreeting)
End Sub
Finally, we have created a Test class containing the Main() method. In the Main() method, we have instantiated the MyForm class and passed its reference to the Application.Run() method so it may receive messages from the Windows Operating System.
When we execute the above code, the following screen is displayed:
To close the application, press the close button on the title bar.
Adding Event Handling
Let's now add a button labeled 'Exit' to the form. The 'Exit' button will close the application when it is clicked. In .NET, Push Buttons are instances of the System.Windows.Forms.Button class. To associate some action with the button click, we need to create an event handler and register (or add) it to the Button's Click event. Below is the code for this application.
Imports System
Imports System.Windows.Forms
Imports System.Drawing
Class Test
Public Sub Main()
Application.Run(New MyForm())
End Sub
End Class
Class MyForm
Inherits Form
Public Sub New()
MyBase.new()
' Form
Me.Text = "My First Windows Application"
Me.Size = New Size(300, 300)
' Label
Dim lblGreeting As New Label()
lblGreeting.Text = "Hello WinForm"
lblGreeting.Location = New Point(100, 100)
' Button
Dim btnExit As New Button()
btnExit.Text = "Exit"
btnExit.Location = New Point(180, 180)
btnExit.Size = New Size(80, 30)
AddHandler btnExit.Click, AddressOf BtnExitOnClick
Me.Controls.AddRange(New Control() {lblGreeting, btnExit})
End Sub
Public Sub BtnExitOnClick(ByVal sender As Object, ByVal e As EventArgs)
Application.Exit()
End Sub
End Class
In the constructor of MyForm, first we have set certain properties of the Form. In this code, we have also used the StartPosition property of the Form, which sets the position of the form on the screen when the application starts. The type of this property is an enumeration called 'FormStartPosition'. We have set the start position of the form to the center of the screen.
The new inclusion in the code is the Exit button called 'btnExit'. We have created the button using the base class System.Windows.Forms.Button. Later, we have set various properties of the button, specifically its text label (Text), its Location and its Size. Finally, we have created an event handler method for this button called BtnExitOnClick(). In the BtnExitOnClick() method, we have written the code to exit the application. We have also subscribed this event handler to the btnExit's Click event (To understand the event handling in VB.Net, see lesson 10 of the VB.Net school). In the end, we have added both the label and the button to the form's Controls collection. Note that this time we have used the AddRange() method of form class to add an array of controls to the Controls collection of form. This method takes an array of type Control as its parameter.
When the code is run, the following window will be displayed:
Now you can press either the Exit Button or the close button at title bar to exit the application.
Alternate procedure for Event Handling – Using the ‘Handles’ Keyword
Note that in the previous code, we subscribed Exit buttons event handler using the ‘AddHandler’ keyword. We can also do this using the ‘Handles’ keyword with the Event Handler method. But to use the ‘Handles’ keyword, the Exit button needs to be instance variable of the form class defined with the ‘WithEvents’ keyword. The complete source code of the MyForm class is
Class MyForm
Inherits Form
' Form controls are usually private
' instance members of the form class
Private WithEvents btnExit As New Button()
Private lblGreeting As New Label()
Public Sub New()
MyBase.new()
' Form
Me.Text = "My First Windows Application"
Me.Size = New Size(300, 300)
' Label
lblGreeting.Text = "Hello WinForm"
lblGreeting.Location = New Point(100, 100)
' Button
btnExit.Text = "Exit"
btnExit.Location = New Point(180, 180)
btnExit.Size = New Size(80, 30)
'AddHandler btnExit.Click, AddressOf BtnExitOnClick
Me.Controls.AddRange(New Control() {lblGreeting, btnExit})
End Sub
Public Sub BtnExitOnClick(ByVal sender As Object, ByVal e As EventArgs) Handles btnExit.Click
Application.Exit()
End Sub
End Class
This procedure of event handling (using the ‘Handles’ keyword) is usually followed by Visual Studio.NET standard code for Form based applications (Windows and Web Applications)
Index