|
Page 1 of 2
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
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.
• 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
|