Export (0) Print
Expand All

AddHandler and RemoveHandler

The AddHandler statement is similar to the Handles clause in that both allow you to specify an event handler that will handle an event. However, AddHandler along with RemoveHandler provide greater flexibility than the Handles clause, allowing you to dynamically add, remove, and change the error handler associated with an event. And unlike Handles, AddHandler allows you to associate multiple event handlers with a single event.

AddHandler takes two arguments: the name of an event from an event sender such as a control, and an expression that evaluates to a delegate. You do not need to explicitly specify the delegate class when using AddHandler, since the AddressOf statement always returns a reference to the delegate. The following example associates an event handler with an event raised by an object:

AddHandler MyObject.Event1, AddressOf Me.MyEventHandler

RemoveHandler, which disconnects an event from an event handler, uses the same syntax as AddHandler. For example:

RemoveHandler MyObject.Event1, AddressOf Me.MyEventHandler

See Also

Addhandler Statement | Events and Event Handlers | WithEvents and the Handles Clause | Writing Event Handlers

Show:
© 2014 Microsoft