This documentation is archived and is not being maintained.

Event Handler Example

The following example defines a class that raises an event when you call the CauseEvent method. This event is handled by an event handler procedure calledEventHandler.

To run this example, add the following code to the form class of a Visual Basic .NET Windows Application project, and call the TestEvents procedure with an integer argument.

Public Class Class1
   ' Declare an event for this class.
   Public Event Event1(ByVal EventNumber As Integer)
   ' Define a method that raises an event.
   Sub CauseEvent(ByVal EventNumber As Integer)
      RaiseEvent Event1(EventNumber)
   End Sub
End Class

Protected Sub TestEvents(ByVal EventNumber As Integer)
   Dim MyObject As New Class1()
   AddHandler MyObject.Event1, AddressOf Me.EventHandler
   ' Cause the object to raise an event.
   MyObject.CauseEvent(EventNumber)
End Sub

Sub EventHandler(ByVal EventNumber As Integer)
   MessageBox.Show("Received event number " & CStr(EventNumber))
End Sub

See Also

Events and Event Handlers | Delegates and the AddressOf Operator | AddHandler and RemoveHandler | Writing Event Handlers

Show: