Step by Step: Event handling in VB.NET






4.63/5 (43 votes)
Step by step guide to implement event handling in VB.NET.
Introduction
In this step by step article, we look at implementing event handling in VB.NET.
Implementing events
-
Step 1 - Create an EventArgs class
If you need to pass arguments to the event handler, a specific
EventArgs
class has to be made. Eventually, a suitableEventArgs
class might already be available, but in most cases, you will have to create one to tailor your specific arguments.If you do not need to pass arguments to the event handler (except the
sender
), then no subclass ofEventArgs
is needed. However, note that you should still define the event method with an argument of typeSystem.EventArgs
, and then passSystem.EventArgs.Empty
.Create the
EventArgs
as follows:Public Class StartEventArgs Inherits System.EventArgs 'Provide one or more constructors, as well as fields and 'accessors for the arguments. End Class
Note: All
EventArgs
classes should have a name ending byEventArgs
. -
Step 2 - Create an event
For each kind of event, an
Event
is created in the sending class:Public Class Sender Public Event Start(ByVal sender As Object, _ ByVal e As StartEventArgs) '... End Class
Note: In VB.NET no explicit declaration of the delegate class is needed.
-
Step 3 - Create OnEvent methods
Although not required, it is a good practice to create protected overridable
OnEvent
methods for your events:Protected Overridable Sub OnStart(ByVal e As StartEventArgs) RaiseEvent Start(Me, e) End Sub
You can now call this method whenever you want to send the
Start
event. i.e.:OnStart(New StartEventArgs(data))
-
Step 4 - Make the event default
When your class represents a component, it is a good practice to make the most commonly used event, the default event. Add a
DefaultEvent
attribute in front of your sender class:<System.ComponentModel.DefaultEvent("Start")> _ Public Class Sender Inherits System.ComponentModel.Component '...
Handling events (static)
Under handling events statically, we understand having a WithEvents
field to the sender.
Any field pointing to an object that can send events, can have the WithEvents
modifier, in which case you can easily write event handlers by creating subroutines with the same signature as the event, and marked as Handles <sender>.<event>
. The name of the method is of no matter, but by default, VS.NET uses <fieldname>_<event>
:
Public Class Receiver
Friend WithEvents MySender As Sender
Private Sub MySender_Start(ByVal sender As Object, _
ByVal e As StartEventArgs) Handles MySender.Start
'...
End Sub
End Class
Handling events (dynamic)
When you have no field to the event sending object, i.e. because the object is passed as parameter to a method that can be called an unspecified number of times, then you can dynamically register to the event using the AddHandler
operation:
AddHandler sender.Start, AddressOf Me.HandleStart
To remove the handler, use the RemoveHandler
operation:
RemoveHandler sender.Start, AddressOf Me.HandleStart
The handling method is a regular subroutine with the same signature as the event (no Handles
is needed):
Public Sub HandleStart(ByVal sender As Object, _
ByVal e As System.EventArgs)
'...
End Sub
Code sample
Public Class StartEventArgs
Inherits System.EventArgs
'Provide one or more constructors, as well as fields and
'accessors for the arguments.
End Class
Public Class Sender
Public Event Start(ByVal sender As Object, ByVal e As StartEventArgs)
Protected Overridable Sub OnStart(ByVal e As StartEventArgs)
RaiseEvent Start(Me, e)
End Sub
'...
End Class
Public Class Receiver
Friend WithEvents MySender As Sender
Private Sub MySender_Start(ByVal sender As Object, _
ByVal e As StartEventArgs) Handles MySender.Start
'...
End Sub
End Class