Introduction to VB.NET Windows Formsby Budi Kurniawan
In VB.NET, Windows forms have changed completely. Now a Windows form is a representation of the
System.Windows.Forms.Form class in the .NET Framework. This article offers an introduction to the new underlying technology by giving examples of how to do the basic tasks of writing a Windows application. This includes creating a form, adding controls, and responding to events. It also gives the object-oriented analysis of a WinForm, concluding with the most powerful new feature that enables code reuse: inheritance.
The new version of VB adds a new feature that entitles it to be called an object-oriented programming language: inheritance. Now, no one can argue that VB is not a purely OOP language. But what does this mean to VB programmers worldwide? Does it mean that every VB programmer must now learn OOP? Well, the answer is not that straightforward. The previous versions of VB have been known for their ease of use. Everyone remembers how easy it was to drag and drop controls and create a Windows application without any understanding of OOP. The half-OOP features in those versions were probably not often used by the majority of programmers. The ease of use continues with the current version; with Visual Studio.NET, creating a VB Windows application is as easy as it used to be. However, this article will show you why you should master OOP to really take advantage of the full power of VB.NET.
A Brief Theory of Inheritance
Everything is an object and there are three main principles of OOP: encapsulation, inheritance and polymorphism. That's how a typical OOP book would teach you. However, this article has no pretension of teaching OOP; it's a broad topic that takes a whole book to explain. This section only gives you a small dose of inheritance, just to make sure that you can still enjoy this article without being an OOP guru. Those who know OOP as well as they know their bodies can skip this section entirely.
If you have a class named
ClassA with a number of methods, you can extend
ClassA into another class. This new class is a child class of
ClassA, and it inherits methods and variables from
ClassA. In the child class, you can use inherited methods and variables of the parent class and/or create your own methods. The advantage is clear: if you want to build a software program and someone else has already written something similar, you can borrow or buy his/her program and extend it. Instead of writing 100% of the application, you can probably release the program with 20% of the work. Imagine how much time and money you can save.
Listing 1 shows three VB.NET classes named
ClassB. The code shows that
ClassB can use the
SendMessage method from
ClassA,as well as its own
ClassC can use the
SendMessage method from
ClassA and the
ClassB, as well as its own
Listing 1: Simple inheritance in VB.NET
Imports System Class ClassA Public Sub SendMessage() Console.WriteLine("From SendMessage in ClassA") End Sub End Class Class ClassB Inherits ClassA Public Sub SendPersonalMessage() Console.WriteLine("From SendPersonalMessage in ClassB") End Sub End Class Class ClassC Inherits ClassB Public Sub SendPrivateMessage() Console.WriteLine("From SendPrivateMessage in ClassC") End Sub End Class Module Module1 Sub Main() Dim a As ClassA = New ClassA() Dim b As ClassB = New ClassB() Dim c As ClassC = New ClassC() a.SendMessage() b.SendMessage() b.SendPersonalMessage() c.SendMessage() c.SendPersonalMessage() c.SendPrivateMessage() End Sub End Module
Now, let's see what it takes to create a form in VB.NET.
Creating a VB.NET Form
VB.NET programmers can use the .NET Framework base type library to rapidly build a Windows application. The
System.Windows.Forms namespace provide types that you need to build a Windows form. To create a form, you simply need to create a new class that inherits the
System.WinForms.Form class. The following code creates a new class named
Form1 that inherits
Public Class Form1 : Inherits System.Windows.Forms.Form End Class
Note that if you put the
Inherits part on a separate line, you don't need a colon that separates the class name and the
Public Class Form1 Inherits System.Windows.Forms.Form End Class
Because when working with forms you will be using classes from the
System.Windows.Forms namespace, you can import the namespace to avoid having to type the namespace in front of every class you use.
Imports System.Windows.Forms Public Class Form1 Inherits System.WinForms.Form End Class
If you use Visual Studio.NET, it will write some initial code. This might be confusing if you don't know which is which. After creating a new VB.NET Windows application, go to the code window of the first form and delete everything and type in the code above. Run the application by pressing F5 and you will see a blank form. Easy, isn't it? How is it possible? Because our
Form1 class inherits the
System.Windows.Forms.Form class, a rich class on its own. The Form class itself is the seventh in the hierarchy. It inherits
ContainerControl that inherits
ScrollableControl, and so on.
See Figure 1 below.
Figure 1: The System.Windows.Forms.Form class hierarcy
However, you might still wonder how it is possible to display a form without a single line of code. The Visual Studio.NET actually takes care of a few things. First, in the
.vbproj file, it specifies the first class to run. This is like having a
hidden Sub Main. By default, the first form (
Form1, in our example) should be the first class to create. Also, when you create a class without a constructor, the VB.NET compiler will assume a no-argument constructor, such as this:
Public Sub New() End Sub
Form1 class actually has a constructor. Explicitly creating the constructor is not a bad idea at all.
Imports System.Windows.Forms Public Class Form1 Inherits System.Windows.Forms.Form Public Sub New() End Sub End Class
What are your experiences with the new VB.NET WinForms?
When you instantiate an object with the
New keyword, one of the class's constructors will be called. In the constructor, you usually write some initialization code. If you have a large number of lines for the initialization code, you may want to create a private sub that takes care of the initialization, to make your code more readable. For example, by default the Visual Studio.NET creates a sub called
InitializeComponent and calls it from inside the constructor.