Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Handles

The Handles keyword is used to declare that a procedure handles a specified event.

proceduredeclaration Handles event

Parts

proceduredeclaration
The Sub procedure declaration for the procedure that will handle the event.
event
The name of the event being handled. This event must be raised by either the base class for the current class, or by an object declared using the WithEvents keyword.

Remarks

Use the Handles keyword at the end of a procedure declaration to cause it to handle events raised by an object variable declared using the WithEvents keyword. The Handles keyword can also be used in a derived class to handle events from a base class.

Example

Public Class ContainerClass
   WithEvents Obj As New Class1()  ' Module or class level declaration.

   Public Class Class1
      Public Event Ev_Event()      ' Declare an event.
      Sub CauseSomeEvent()
         RaiseEvent Ev_Event()     ' Raise an event.
      End Sub
   End Class

   Sub EventHandler() Handles Obj.Ev_Event
      MsgBox("EventHandler caught event.")   ' Handle the event.
   End Sub

' Call the TestEvents procedure from an instance of the ContainerClass 
' class to test the Ev_Event event and the event handler.
   Public Sub TestEvents()    
      Obj.CauseSomeEvent()
   End Sub
End Class

The following example demonstrates how a derived class can use the Handles statement to handle an event from a base class.

Public Class BaseClass
   Event Ev1() ' Declare an event.
End Class
Class DerivedClass
   Inherits BaseClass
   Sub TestEvents() Handles MyBase.Ev1
      ' Add code to handle this event.
   End Sub
End Class

See Also

WithEvents | AddHandler Statement | RemoveHandler Statement | Events and Event Handlers

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft