Share via


How to: Write 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 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.

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

  1. Create a simple class that contains an event.

    Class Class1
        Public Event AnEvent(ByVal EventNumber As Integer)
    End Class
    
    Class Class1
        Public Event AnEvent(ByVal EventNumber As Integer)
    End Class
    
  2. In the class or 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
    
    Public WithEvents ClassInst As Class1
    
  3. In the Code Editor, choose the WithEvents variable you just declared from the Class Name drop-down list on the left.

  4. 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.

  5. 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 Integer) _
          Handles ClassInst.AnEvent
        MsgBox("Received event number: " & CStr(EventNumber))
    End Sub
    
    Public Sub ClassInst_AnEvent(ByVal EventNumber As Integer) _
          Handles ClassInst.AnEvent
        MsgBox("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

  1. Create a subroutine to handle the event, as in the following example:

    Public Sub EHandler(ByVal EventNumber As Integer)
        MsgBox("Received event number " & CStr(EventNumber))
    End Sub
    
    Public Sub EHandler(ByVal EventNumber As Integer)
        MsgBox("Received event number " & CStr(EventNumber))
    End Sub
    
  2. 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:

    Public Sub TestAddHandler()
        Dim CI As New Class1
    End Sub
    
    Public Sub TestAddHandler()
        Dim CI As New Class1
    End Sub
    
  3. 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, add the following code to the end of the TestAddHandler subroutine:

    AddHandler CI.AnEvent, AddressOf EHandler
    
    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.

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
    
    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 HandlesMyBase statement.

To handle events from a base class

  • Declare an event handler in the derived class by adding a Handles MyBase.eventname statement to the declaration line of your event-handler procedure, where eventname is the name of the event in the base class you are handling. For example:

    Public Class BaseClass
        Public Event BaseEvent(ByVal i As Integer)
        ' Place methods and properties here. 
    End Class 
    
    Public Class DerivedClass
        Inherits BaseClass
        Sub EventHandler(ByVal x As Integer) Handles MyBase.BaseEvent
            ' Place code to handle events from BaseClass here. 
        End Sub 
    End Class
    
    Public Class BaseClass
        Public Event BaseEvent(ByVal i As Integer)
        ' Place methods and properties here. 
    End Class 
    
    Public Class DerivedClass
        Inherits BaseClass
        Sub EventHandler(ByVal x As Integer) Handles MyBase.BaseEvent
            ' Place code to handle events from BaseClass here. 
        End Sub 
    End Class
    

See Also

Concepts

Events and Event Handlers

Delegates and the AddressOf Operator

AddHandler and RemoveHandler

Reference

Handles

AddHandler Statement