WinForms and Windows Applications
If you are new to VB.Net School
This is the 11th in the series of lessons in the VB.Net School. The VB.Net School is a kind of interactive learning platform where those who want to learn .NET with VB.Net can find help and support. With one issue a week, describing some areas of the VB.Net Programming Language with the Microsoft .Net Platform, this is not the same traditional passive tutorial where the author only writes and the reader only reads. There will be exercise problems at the end of each issue, which the reader is expected to solve after reading the issue. The solution to these problems will be provided in the next issue for testing purposes. There is also a dedicated
message board attached with the school, where you can ask questions about the article, and the author will respond to your question within 2/3 days. You can send your suggestions, feedback or ideas on how these lessons can be improved to either the Author (
farazrasheed@acm.org) or the WEBMASTER (
info@programmersheaven.com).
For previous lessons click
here
Lesson Plan
Today we will start building Windows Applications in VB.Net. We will start by looking at the architecture of Windows Applications and their support in .Net. Later we will design our first "Hello WinForm" Application and learn about various windows form controls. Finally we will look at how Visual Studio.Net eases the creation of Windows Applications.
Windows Applications and .Net
VB.Net and .Net provide extensive support for building Windows Applications. The most important point about windows applications is that they are 'event driven'. All windows applications present a graphical interface to their users and respond to user interaction. This graphical user interface is called a 'Windows Form' or a 'WinForm' for short. A windows form may contain text labels, push buttons, text boxes, list boxes, images, menus and a vast range of other controls. In fact, a WinForm is also a windows control just like a text box, label, etc. In .Net all windows controls are represented by base class objects contained in the System.Windows.Forms namespace.
WinForm Basics
As stated earlier, .Net provides the WinForm and other controls through base classes in the System.Windows.Forms namespace. The class System.Windows.Forms.Form is the base class of all WinForms in .Net. In order to design a windows application, we need to:
1.Create a Windows Application project in Visual Studio.Net, or add references to System.Windows.Forms and System.Drawing to your current project. If you are not using Visual Studio at all, use the /reference option of the command line compiler to add these assemblies.
2. Write a new class to represent the WinForm and derive it from the System.Windows.Forms.Form class:
Public Class MyForm
Inherits System.Windows.Forms.Form
...
End Class
3. Instantiate various controls, set their appropriate properties and add these to MyForm's Controls collection.
4. Write another class containing the Main() method. In the Main() method, call the System.Application.Run() method, supplying it with an instance of MyForm.
Class Test
Public Sub Main()
Application.Run(New MyForm())
End Sub
End Class
The Application.Run() method registers your form as a windows application in the operating system so that it may receive event messages from the Windows Operating System.
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 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. Forms, Labels and Buttons. Finally, including the System.Drawing namespace is necessary as it contains the classes related to the drawing of the 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 typing the Imports statement for them. We also need to add references to these assemblies (for now think of assemblies as Windows DLL's or library files for .Net) that contain the code of these namespaces. For this we need to add a reference to the System.Windows.Forms.dll and the System.Drawing.dll. To add a reference, right click the 'Reference' folder under the current project in the solution explorer and select Add Reference... It will show the following window
http://www.programmersheaven.com/articles/faraz/vbnet/lesson11_img1.gif
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)
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 a 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:
http://www.programmersheaven.com/articles/faraz/lesson11_img1.gif
To close the application, press the close button on the title bar.
School Home