How event handling is implemented in win form and .NET form controls with VB.NET?

VB.NET Version

Adding Event Handling A control exposes an event by defining its ‘delegate’. We can add our own event handler for the event by writing an event handler method and adding it to the event's delegate. Let's 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


The interesting lines defining the event handler for the button is highlighted with bold formatting. 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). 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


Note that the button is now instantiated with the ‘WithEvents’ keyword and the event handler is specifically marked with the ‘Handles’ keyword to qualify it as an event handler method. This procedure of implementing event handling (using the ‘Handles’ keyword) is usually followed by Visual Studio.NET standard code for Form based applications (Windows and Web Applications)

Index

 
Printer friendly version of the FAQ-WinForm-VB-Event-handlers page


Sponsored links

.Net Application Updating
One easy to use component adds safe and reliable updating features. Download today for a free trial.
Build IT Knowledge with Current & Trusted Content
Helps Employees Develop & Hone New Technical Programming Skills. Sign Up & Get Full Access.
Check Out IT Certification Preparation Materials
Sign Up With SkillSoft & Get Access to Training Materials for Over 50 Professional Certifications.
Online Crash Analysis
Automatically capture customer crash data, no debugger required. Support for .NET, C++, OS X, Java.
.Net Localization in three simple steps (WYSIWYG)
Localize .Net, C#/C/C++ & Delphi apps visually. HTML, HTML Help, XML & databases. Try Sisulizer now!

Advertisement



Free Magazine

Free Magazines
eWeek The essential technology information source for builders of e-business.... subscribe now

Newsletter | Submit Content | About | Advertising | Awards | Contact Us | Link to us |
© 1996-2008 Community Networks Ltd All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Synchron Data - .NET development.