Exforsys

VB.NET 2005

  1. VB.NET 2005 Free Training
  2. The .NET Framework Architecture Part 1
  3. The .NET Framework Architecture Part 2
  4. Application Class and Message Class
  5. Implementing Class Library Object
  6. Visual Studio.NET Namespaces
  7. .NET Assemblies
  8. Differences between VB.NET 1.0 and VB.NET 2.0
  9. Introducing VB.NET Windows Forms
  10. Visual Studio Windows Forms Designer
  11. Exploring the Forms Designer generated code
  12. Setting and Adding Properties to Windows Form
  13. Implementing Inheritance
  14. Event Handling In Visual Basic .NET
  15. Building Graphical Interface elements
  16. .NET Common Windows Forms Controls Part 1
  17. .NET Common Windows Forms Controls Part 2
  18. Common Controls and Handling Control Events
  19. DomainUpDown and NumericUpDown Controls
  20. Dialog Boxes in Visual Basic .NET
  21. Visual Studio Adding Controls to Windows Form
  22. VB.NET Validation Controls
  23. Working with Menu Controls
  24. VB.NET MDI Applications
  25. .NET Exceptions
  26. VB.NET Creating and Managing Components Part 1
  27. VB.NET Creating and Managing Components Part 2
  28. Simple Data Binding
  29. .NET Complex Data Binding
  30. .NET Data Form Wizard
  31. Data Manipulation with ADO.NET
  32. SQL Server Stored Procedures
  33. SQL Server Ad Hoc Queries
  34. Finding and Sorting Data in DataSets
  35. ADO.NET Object Model
  36. Working with DataSets
  37. Using XML Data
  38. Working with File System in .NET
  39. Creating Web Service
  40. Instantiating - Invoking Web Services, Creating Proxy Classes with WSDL
  41. Web Reference and Web Services
  42. Web Services - SOAP, WSDL, Disco and UDDI
  43. Web Application Testing in VB.NET 2005
  44. Web Application Tracing and Debugging
  45. Working with Legacy Code and COM Components
  46. ActiveX Controls and Legacy Code
  47. Windows Application Testing
  48. VB.NET Windows Application Testing
  49. Tracing VB.NET Windows Application
  50. Debugging Windows Applications In Visual Studio.NET 2005
  51. Deploying Windows Applications In Visual Studio.NET 2005
  52. Customizing Setup Project in Visual Studio.NET 2005
  53. Shared Assembly
  54. Microsoft .NET Creating Installation Components
  55. The Registry Editor in Visual Studio.NET 2005
  56. The File Types Editor

Ads


Home arrow Technical Training arrow VB.NET 2005

Event Handling In Visual Basic .NET

Page 1 of 2
Author : Exforsys Inc.     Published on: 11th Jun 2005    |   Last Updated on: 24th Dec 2007

Introduction to Event Handling

One of the most useful capabilities of the OOP Languages is their inbuilt ability to be aware of a large number of events like MouseOver, MouseClick, and so on so that we can write codes to react to any event that we are interested. This is made possible by the rich set of classes that have been built in the .NET Framework

Ads

The events handling is very simple as we have seen in the previous example where buttonClicked event is handled by the following code:

Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click

The Handles keyword is used to tell the program to look for the specific event. The sub or the function that handles the event will be called immediately when the event occurs. This is a simple implementation. In some cases you may have to raise an application event which will be caught by the event handler. Please see the following code fragment to understand this.

Public Event TimeExpired(ByVal Status As String)

RaiseEvent TimeExpired("Your time has run out")

The first line declares the event Time expired and the second statement raises the event. In order that this code works you need to make a reference to the System namespace. The Event statement should be located outside any procedure or Sub and at the class level. The RaiseEvent statement should be available inside some procedure in the application. Events must be raised within the scope of the class or module or structure where they are declared. Thus you cannot raise an event declared in the base class from within a derived class. The object that raised the event is the sender or the source of the event. Some of the examples of these kinds of objects are forms, controls etc.

An event handler can be any procedure that is called when the corresponding event occurs. You cannot use a function as an event handler as it would necessitate the function to return a value to the source object. The event handler cannot be made use of unless you first associate the procedure with the event by using the keyword Handles or AddHandler statement.

While handles clause is used with design time activity AddHandler and RemoveHandler statements are more flexible and they allow you to dynamically connect or disconnect the events with one or more event handlers at run time. You are also relieved from adding one more line of code using withEvents.

Let us see an example of event handling using Handles clause.

Dim withEvents AnEvent as new EventRaised()
Sub EventEgs()
AnEvent.RaiseEvents()
End Sub

Sub AnEvent_EventHandler() Handles AnEvent.EventOne, AnEvent.EventTwo
MsgBox(“Received Event”)
End Sub

Class EventRaised
Public Event EventOne()
Public Event EventTwo()
Sub RaiseEvents()
RaiseEvent EventOne()
RaiseEvent EventTwo()
End Sub
End Class

There are some limitations in using this kind of solution as listed below:

• You cannot use a WithEvents variable as a object variable. That is, you cannot declare it as Object — you must specify the class name when you declare the variable.

• You cannot use WithEvents to declaratively handle shared events, since they are not tied to an instance that can be assigned to a WithEvents variable. Similarily, you cannot use WithEvents or Handles to handle events from a Structure. In both cases, you can use the AddHandler statement to handle those events.

Ads

• You cannot create arrays of WithEvents variables.

• WithEvents variables allow a single event handler to handle one or more kind of event, or one or more event handlers to handle the same kind of event.

The other method is employing the AddHandler and RemoveHandler clause. We shall see this in the next section



 
This tutorial is part of a VB.NET 2005 tutorial series. Read it from the beginning and learn yourself.

VB.NET 2005

  1. VB.NET 2005 Free Training
  2. The .NET Framework Architecture Part 1
  3. The .NET Framework Architecture Part 2
  4. Application Class and Message Class
  5. Implementing Class Library Object
  6. Visual Studio.NET Namespaces
  7. .NET Assemblies
  8. Differences between VB.NET 1.0 and VB.NET 2.0
  9. Introducing VB.NET Windows Forms
  10. Visual Studio Windows Forms Designer
  11. Exploring the Forms Designer generated code
  12. Setting and Adding Properties to Windows Form
  13. Implementing Inheritance
  14. Event Handling In Visual Basic .NET
  15. Building Graphical Interface elements
  16. .NET Common Windows Forms Controls Part 1
  17. .NET Common Windows Forms Controls Part 2
  18. Common Controls and Handling Control Events
  19. DomainUpDown and NumericUpDown Controls
  20. Dialog Boxes in Visual Basic .NET
  21. Visual Studio Adding Controls to Windows Form
  22. VB.NET Validation Controls
  23. Working with Menu Controls
  24. VB.NET MDI Applications
  25. .NET Exceptions
  26. VB.NET Creating and Managing Components Part 1
  27. VB.NET Creating and Managing Components Part 2
  28. Simple Data Binding
  29. .NET Complex Data Binding
  30. .NET Data Form Wizard
  31. Data Manipulation with ADO.NET
  32. SQL Server Stored Procedures
  33. SQL Server Ad Hoc Queries
  34. Finding and Sorting Data in DataSets
  35. ADO.NET Object Model
  36. Working with DataSets
  37. Using XML Data
  38. Working with File System in .NET
  39. Creating Web Service
  40. Instantiating - Invoking Web Services, Creating Proxy Classes with WSDL
  41. Web Reference and Web Services
  42. Web Services - SOAP, WSDL, Disco and UDDI
  43. Web Application Testing in VB.NET 2005
  44. Web Application Tracing and Debugging
  45. Working with Legacy Code and COM Components
  46. ActiveX Controls and Legacy Code
  47. Windows Application Testing
  48. VB.NET Windows Application Testing
  49. Tracing VB.NET Windows Application
  50. Debugging Windows Applications In Visual Studio.NET 2005
  51. Deploying Windows Applications In Visual Studio.NET 2005
  52. Customizing Setup Project in Visual Studio.NET 2005
  53. Shared Assembly
  54. Microsoft .NET Creating Installation Components
  55. The Registry Editor in Visual Studio.NET 2005
  56. The File Types Editor
 

Comments