Click here to Skip to main content
Click here to Skip to main content

Step by Step: Event handling in VB.NET

, 22 Sep 2003 Public Domain
Rate this:
Please Sign up or sign in to vote.
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 suitable EventArgs 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 of EventArgs is needed. However, note that you should still define the event method with an argument of type System.EventArgs, and then pass System.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 by EventArgs.

  • 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

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication

Share

About the Author

Rudi Breedenraedt
Architect Wolters Kluwer Belgium
Belgium Belgium
Rudi is a Software Architect at Wolters Kluwer Belgium.

Comments and Discussions

 
GeneralMy vote of 5 PinmemberGuyThiebaut18-Jul-13 3:10 
GeneralRe: My vote of 5 PinmemberRudi Breedenraedt18-Jul-13 5:58 
GeneralMy vote of 3 PinmemberPallini1-Oct-12 23:18 
Still I have no idea how it all works Smile | :)
GeneralRe: My vote of 3 PinmemberRudi Breedenraedt1-Oct-12 23:55 
GeneralRe: My vote of 3 PinmemberPallini2-Oct-12 0:00 
GeneralNice article PinmemberBhati_1_Vipin17-Sep-12 21:13 
QuestionEvent Handling? Pinmemberbhavna81619-Jan-07 1:47 
Questionevent handling in service Pinmembertomsmaily12325-Aug-06 5:51 
GeneralGood one for beginners Pinmembernoby_datasoft15-Apr-04 17:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.1411022.1 | Last Updated 23 Sep 2003
Article Copyright 2003 by Rudi Breedenraedt
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid