Introduction to VB.NET Windows Forms
Pages: 1, 2, 3
Event Handling
The previous versions of VB are event-driven; you can write code that responds to an event easily. For instance, if you want a button called Button1 to respond to the user click, all you need to do is to create an event procedure called Button1_Click. (Well, actually the VB IDE does this for you.) In VB.NET it's business as usual, in regard to event handling. Once you draw your control in the Design mode, you can click it to tell Visual Studio.NET that you want to write an event procedure. Here you will be shown how to write it manually, though, so you get the picture of how events really work in VB.NET.
If you want your object to respond to an event, you use the keyword WithEvents when you declare it. For example, if you want your button to be able to respond to events, instead of saying
Private Button1 As Button
you add the keyword WithEvents before the variable name:
Private WithEvents Button1 As Button
Then, you need to create an event procedure for each event that your object should respond to. For Button1 to respond to a click event, you need a Button1_Click sub, followed by Handles Control.Event.
Private Sub Button1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Button1.Click
'Write your code here
End Sub
The Form class and all the control classes inherit the events from the Control class. If you look up the .NET Framework reference for the System.Windows.Forms.Control class, you will see that this class has 58 events, including the Disposed event, inherited from Component. To name a few more: Click, DoubleClick, GotFocus, LostFocus, KeyUp, KeyDown, etc.
The code in Listing 3 creates a form and adds two controls, a Button and a TextBox, to it. The Button is declared with the WithEvents keyword. For this control, two event procedures are created: Button1_Click and Button1_GotFocus.
Listing 3: Event Handling in a Windows Form
Imports System.Windows.Forms
Public Class Form1
Inherits Form
Private WithEvents Button1 As Button
Private TextBox1 As TextBox
Public Sub New()
InitializeComponent()
End Sub
Private Sub InitializeComponent()
Me.Text = "Developer Form"
Me.Width = 400
Me.Height = 300
Button1 = New Button()
TextBox1 = New TextBox()
Button1.Left = 200
Button1.Top = 200
Button1.Width = 100
Button1.Height = 40
Button1.TabIndex = 0
Button1.Text = "Click Me"
TextBox1.Left = 200
TextBox1.Top = 30
TextBox1.Width = 150
TextBox1.Height = 40
Me.Controls.Add(Button1)
Me.Controls.Add(TextBox1)
End Sub
Private Sub Button1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Button1.Click
TextBox1.Text = "Ooops.... you clicked me."
End Sub
Private Sub Button1_GotFocus(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Button1.GotFocus
TextBox1.Text = "Button got focus."
End Sub
End Class
Form Inheritance
Now, this is the most important part of this article. This section explains a very powerful feature of inheritance. When you create a new form, you extend the System.WinForms.Form class. Now that VB.NET supports inheritance,
you can also extend your own form. This way, if you want to create a form that is similar to a form you have created before, you can just extend it and save time.
You may have guessed that it would be something like this:
Public Class NewForm
Inherits WindowsApplication1.ExistingForm
The following example shows form inheritance. You have created an Employee form that has two labels, two textboxes, and two buttons. (See Figure 4; you basically need an employee's first name and last name.)
|
Now you need to create a form for all contractors in your company. Contractors are like normal employees. They come to the office everyday and, like the rest of us, use the office microwave to warm their lunch. The only difference is that they are not directly paid by your company. Your company pays an external company for their service.
Now, rather than create a new form for the contractors, you can extend the existing Employee form. A contractor has a first name and last name just like an employee, but in addition, a contractor has an external company field. The code for the existing Employee form is given in Listing 4. Note that the code is generated by Visual Studio.NET. By now, however, you should not have a problem understanding it.
Listing 4: The Employee Form
Imports System.Drawing
Imports System.Windows.Forms
Imports System.ComponentModel
Public Class Form2
Inherits Form
Public Sub New()
MyBase.New()
'This call is required by the Win Form Designer.
InitializeComponent()
End Sub
#Region " Windows Form Designer generated code "
'Required by the Windows Form Designer
Private components As System.ComponentModel.Container
Protected cancel As System.Windows.Forms.Button
Private LastName As System.Windows.Forms.TextBox
Private FirstName As System.Windows.Forms.TextBox
Private Label2 As System.Windows.Forms.Label
Private Label1 As System.Windows.Forms.Label
Protected ok As System.Windows.Forms.Button
Dim Form2 As System.Windows.Forms.Form
Private Sub InitializeComponent()
Me.Label1 = New System.Windows.Forms.Label()
Me.FirstName = New System.Windows.Forms.TextBox()
Me.ok = New System.Windows.Forms.Button()
Me.Label2 = New System.Windows.Forms.Label()
Me.LastName = New System.Windows.Forms.TextBox()
Me.cancel = New System.Windows.Forms.Button()
Me.SuspendLayout()
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(8, 24)
Me.Label1.Name = "Label1"
Me.Label1.TabIndex = 1
Me.Label1.Text = "First Name"
'
'FirstName
'
Me.FirstName.Location = New System.Drawing.Point(120, 16)
Me.FirstName.Name = "FirstName"
Me.FirstName.Size = New System.Drawing.Size(136, 20)
Me.FirstName.TabIndex = 3
Me.FirstName.Text = ""
'
'ok
'
Me.ok.Location = New System.Drawing.Point(8, 128)
Me.ok.Name = "ok"
Me.ok.Size = New System.Drawing.Size(112, 32)
Me.ok.TabIndex = 0
Me.ok.Text = "OK"
'
'Label2
'
Me.Label2.Location = New System.Drawing.Point(8, 48)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(88, 16)
Me.Label2.TabIndex = 2
Me.Label2.Text = "Last Name"
'
'LastName
'
Me.LastName.Location = New System.Drawing.Point(120, 40)
Me.LastName.Name = "LastName"
Me.LastName.Size = New System.Drawing.Size(136, 20)
Me.LastName.TabIndex = 4
Me.LastName.Text = ""
'
'cancel
'
Me.cancel.Location = New System.Drawing.Point(144, 128)
Me.cancel.Name = "cancel"
Me.cancel.Size = New System.Drawing.Size(112, 32)
Me.cancel.TabIndex = 5
Me.cancel.Text = "Cancel"
'
'Form2
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(272, 181)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.cancel, Me.LastName, Me.FirstName, Me.Label2, Me.Label1, Me.ok})
Me.Name = "Form2"
Me.Text = "Employee Form"
Me.ResumeLayout(False)
End Sub
#End Region
End Class
Note that all controls are declared Private except the OK and Cancel Button controls that are declared Protected. By declaring a control Protected, you have access to it from any class that inherits the Employee Form. The code in Listing 5 is the Contractor form that extends the Employee form. What it does is inherit the Employee form and add two more controls (Label3 and ExternalCompany). Note also that we change the OK button position a bit, to show that we have access to this button.
Listing 5: The Contractor form
Imports System.Windows.Forms
Public Class Form1
Inherits WindowsApplication1.Form2
Private Label3 As Label
Private ExternalCompany As TextBox
Public Sub New()
InitializeComponent()
End Sub
Private Sub InitializeComponent()
Label3 = New Label()
ExternalCompany = New TextBox()
Label3.Location = New System.Drawing.Point(8, 72)
Label3.Size = New System.Drawing.Size(88, 16)
Label3.Text = "Ext Company"
ExternalCompany.Location = New System.Drawing.Point(120, 62)
ExternalCompany.Size = New System.Drawing.Size(136, 20)
' Change the ok button position
ok.Location = New System.Drawing.Point(8, 140)
Me.Text = "Contractor Form"
Me.Controls.Add(Label3)
Me.Controls.Add(ExternalCompany)
End Sub
End Class
Figure 5 shows the Contractor form.
|
Summary
You have seen the new Windows form in VB.NET. It's as easy as in the previous versions of VB. However, it's now fully OOP, and understanding OOP will give you access to the full power of VB.NET. For instance, understanding inheritance enables you to extend a form and maximize code reuse. See my next article for details.
Budi Kurniawan is a senior J2EE architect and author.
Return to the .NET DevCenter.



