Click here to Skip to main content
Licence Public Domain
First Posted 22 Sep 2003
Views 217,613
Bookmarked 87 times

Step by Step: Event handling in VB.NET

By | 22 Sep 2003 | Article
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

About the Author

Rudi Breedenraedt

Architect
RealDolmen
Belgium Belgium

Member

Rudi is a Technical Architect at RealDolmen.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionEvent Handling? Pinmemberbhavna8160:47 19 Jan '07  
Questionevent handling in service Pinmembertomsmaily1234:51 25 Aug '06  
GeneralGood one for beginners Pinmembernoby_datasoft16:52 15 Apr '04  

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.

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