Writing Event Handlers
The way you construct an event handler depends on how you want to associate it with events. The standard way to create an event handler is to use the Handles keyword with the WithEvents keyword. Visual Basic .NET provides a second way to handle events: the AddHandler statement. AddHandler and RemoveHandler allow you to dynamically start and stop event handling for a specific event. You can use either approach, but you should not use both WithEvents and AddHandler with the same event.
Handling Events Using WithEvents
The WithEvents keyword allows you to create class or module-level object variables that can be used with the Handles clause in event handlers.
To handle events using WithEvents and the Handles clause
- In the Declarations section of the module that will handle the event, use the WithEvents keyword to declare an object variable for the source of your events, as in the following example:
Public WithEvents ClassInst As Class1
- In the Code editor, choose the WithEvents variable you just declared from the Class Name drop-down list on the left.
- Choose the event you want to handle from the Method Name drop-down list on the right. The Code editor creates the empty event handler procedure with a Handles clause.
Note This step is optional. You can create the event handler procedure manually as long as the procedure you create is a subroutine, has the correct argument list to match the event being handled, and has a Handles clause that specifies the event being handled.
- Add event-handling code to the event handler procedure using the supplied arguments. The following code provides an example:
Public Sub ClassInst_AnEvent(ByVal EventNumber As System.Integer) _ Handles ClassInst.AnEvent MessageBox.Show("Received event number: " & CStr(EventNumber)) End Sub
Handling Events Using AddHandler
You can use the AddHandler statement to dynamically connect events with event handler procedures.
To handle events using AddHandler
- Declare an object variable of the class that is the source of the events you want to handle. Unlike a WithEvents variable, this can be a local variable in a procedure; for example:
Dim CI As New Class1()
- Use the AddHandler statement to specify the name of the event sender, and the AddressOf statement to provide the name of your event handler; for example:
AddHandler CI.AnEvent, AddressOf EHandler
Any procedure can serve as an event handler as long as it supports the correct arguments for the event being handled.
- Add code to the event handler, as in the following example:
Public Sub EHandler(ByVal EventNumber As Integer) MessageBox.Show("Received event number " & CStr(EventNumber)) End Sub
Using RemoveHandler to Stop Handling Events
You can use the RemoveHandler statement to dynamically disconnect events from event handler procedures.
To stop handling events using RemoveHandler
- Use the RemoveHandler statement to specify the name of the event sender, and the AddressOf statement to provide the name of your event handler. The syntax for RemoveHandler statements will always closely match the AddHandler statement used to start event handling; for example:
RemoveHandler CI.AnEvent, AddressOf EHandler
Handling Events Inherited from a Base Class
Derived classes — classes that inherit characteristics from a base class — can handle events raised by their base class using the Handles MyBase statement.
To handle events from a base class
- Declare an event handler in the derived class by adding a Handles MyBase.<event name> statement to the declaration line of your event handler procedure, where event name is the name of the event in the base class you are handling. For example:
Public Class Class1 Public Event SE(ByVal i As Integer) ' Place methods and properties here. End Class Public Class Class2 Inherits Class1 Sub EventHandler(ByVal x As Integer) Handles MyBase.SE ' Place code to handle events from Class1 here. End Sub End Class