What is the purpose of AddHandler keyword?
- The AddHandler statement allows you to specify an event handler.
- Addhandler associates an event with its handler at runtime.
- AddHandler has to be used to handle shared events or events from a structure.
The arguments passed to the Addhandler are:a. The name of an event from an event sender.
b. An expression that evaluates to a delegate.
Example:Sub MyEvents()
Dim myObj As New MyClass
' Associate an event handler with an event.
AddHandler myObj.MyEvent, AddressOf MyEventHandler
' Call the method to raise the event.
myObj.ExecuteSomeEvent()
' Stop handling events.
RemoveHandler myObj.MyEvent, AddressOf MyEventHandler
' This event will not be handled.
myObj.ExecuteSomeEvent()
End Sub
Sub MyEventHandler()
' Handle the event.
MsgBox("MyEventHandler caught event.")
End Sub
Public Class MyClass
' Declare an event.
Public Event MyEvent()
Sub ExecuteSomeEvent()
' Raise an event.
RaiseEvent MyEvent()
End Sub
End Class