Click here to Skip to main content
15,868,016 members
Articles / Programming Languages / Visual Basic

Add Custom Event to a Class in VB.NET

Rate me:
Please Sign up or sign in to vote.
4.88/5 (13 votes)
21 Jul 2011CPOL2 min read 101.3K   3.2K   29   7
Add Custom Event to a Class

Introduction

This article demonstrates how to add a Custom Event to a Class in VB.NET and use it by any means in your code.

On January 2010, I wrote an article in Arabic language about how to handle Custom Event in VB.NET either within a Class or within a Component/Control and I would like to share the same thoughts with my CodeProject colleagues. It might be helpful for somebody.

Background

According to MSDN to handle an Event or more than an Event, the following procedures shall be followed:

  • Define a delegate collection within the class that raises the events.
  • Define a key for each event.
  • Define the event properties in the class that raises the events.
  • Use the delegate collection to implement add and remove accessor methods for the event properties.
  • Use the public event properties to add and remove event handler delegates in the classes that handle the events.

Actually adding or handling a Public Event within a Class is a very simple issue, but handling a Custom Event with a Class differs a little as it requires a Delegate List such as EventHandlerList Class where you have to store your Custom Event inside it.

Why Custom Event

  • Avoid Blocking, as it is important that one EventHandler not block the subsequent EventHandlers.
  • Conserve Memory, as Custom events allow the application to use memory only for the events that it handles.

Handle Custom Event within Component/Control

Handling Custom Event within a Component/Control is a simple and straightforward job and the following steps show how to do it.

  • Define the event to use the delegate store, just give it a suitable unique name.
    VB.NET
    Public Custom Event AnyName As EventHandler
        AddHandler(ByVal value As EventHandler)
    
        End AddHandler
    
        RemoveHandler(ByVal value As EventHandler)
    
        End RemoveHandler
    
        RaiseEvent(ByVal sender As Object, ByVal e As System.EventArgs)
    
        End RaiseEvent
    End Event 
  • Add the delegate to the component EventHandlerList. Note that you must use unique string for each Event.
    VB.NET
    Public Custom Event AnyName As EventHandler
        AddHandler(ByVal value As EventHandler)
            Me.Events.AddHandler("AnyNameEvent", value)
        End AddHandler
    
        RemoveHandler(ByVal value As EventHandler)
    
        End RemoveHandler
    
        RaiseEvent(ByVal sender As Object, ByVal e As System.EventArgs)
    
        End RaiseEvent
    End Event
  • Remove the delegate from the component’s EventHandlerList.
    VB.NET
    Public Custom Event AnyName As EventHandler
        AddHandler(ByVal value As EventHandler)
            Me.Events.AddHandler("AnyNameEvent", value)
        End AddHandler
    
        RemoveHandler(ByVal value As EventHandler)
            Me.Events.RemoveHandler("AnyNameEvent", value)
        End RemoveHandler
    
        RaiseEvent(ByVal sender As Object, ByVal e As System.EventArgs)
    
        End RaiseEvent
    End Event 
  • Raise the event.
    VB.NET
    Public Custom Event AnyName As EventHandler
        AddHandler(ByVal value As EventHandler)
            Me.Events.AddHandler("AnyNameEvent", value)
        End AddHandler
    
        RemoveHandler(ByVal value As EventHandler)
            Me.Events.RemoveHandler("AnyNameEvent", value)
        End RemoveHandler
    
        RaiseEvent(ByVal sender As Object, ByVal e As System.EventArgs)
            CType(Me.Events("AnyNameEvent"), EventHandler).Invoke(sender, e)
        End RaiseEvent
    End Event 
  • Write the method to call the event, and then you may use it as you want.
    VB.NET
    Imports System.ComponentModel
    
    Public Class CustomEventComponent_Sample
        Inherits Component
    
        ' Define the Click event to use the delegate store.
        Public Custom Event AnyName As EventHandler
            AddHandler(ByVal value As EventHandler)
                ' Add the delegate to the Component's EventHandlerList Collection
                Me.Events.AddHandler("AnyNameEvent", value)
            End AddHandler
    
            RemoveHandler(ByVal value As EventHandler)
                ' Remove the delegate from the Component's EventHandlerList Collection
                Me.Events.RemoveHandler("AnyNameEvent", value)
            End RemoveHandler
    
            RaiseEvent(ByVal sender As Object, ByVal e As System.EventArgs)
                ' Raise the event.
                CType(Me.Events("AnyNameEvent"), EventHandler).Invoke(sender, e)
            End RaiseEvent
        End Event
    
        ' Write the method to call the Event, and then use it as you want.
        Protected Sub OnAnyName(ByVal e As EventArgs)
            Dim anyNameHandler As EventHandler = _
    		CType(Me.Events("AnyNameEvent"), EventHandler)
            If (anyNameHandler IsNot Nothing) Then
                anyNameHandler.Invoke(Me, e)
            End If
        End Sub
    
    End Class

Handle Custom Event within a Class

To handle Custom Event within any Class, only we need a collection to store the Delegates within it, for example, you could use a Hashtable class or ArrayList Class or any similar collection, you may even use the EventHandlerList Class.
The following steps define how to handle a Custom Event within a Class and how to Dispose It.

  • Define Your Class and implements IDisposable Interface.
    VB.NET
    Public Class ThemeColors
        Implements IDisposable
    
        ' rest of the code goes here
    
    End Class
  • Define a Collection to store the delegates within it.
    VB.NET
    Private _events As EventHandlerList = Nothing
        Protected ReadOnly Property Events() As EventHandlerList
            Get
                If _events Is Nothing Then
                    _events = New EventHandlerList()
                End If
                Return _events
            End Get
        End Property
  • As explained above, define the event to use the delegate store, add the delegate to the EventHandlerList, remove the delegate from the EventHandlerList and finally raise the event, finally write the method to call the event.
    VB.NET
    #Region " Events "
         Public Custom Event ThemeChanged As EventHandler
            AddHandler(ByVal value As EventHandler)
                Me.Events.AddHandler("ThemeChangedEvent", value)
            End AddHandler
    
            RemoveHandler(ByVal value As EventHandler)
                Me.Events.RemoveHandler("ThemeChangedEvent", value)
            End RemoveHandler
    
            RaiseEvent(ByVal sender As Object, ByVal e As System.EventArgs)
                CType(Me.Events("ThemeChangedEvent"), EventHandler).Invoke(sender, e)
            End RaiseEvent
        End Event
    
    #End Region
    #Region " Methods "
         Protected Overridable Sub OnThemeChanged_
    		(ByVal sender As Object, ByVal e As EventArgs)
            Dim handler As EventHandler = _
    		CType(Me.Events("ThemeChangedEvent"), EventHandler)
            If (handler IsNot Nothing) Then
                RaiseEvent ThemeChanged(sender, e)
            End If
        End Sub
    
    #End Region

Handle Custom Event within a Collection

Nothing to add, just do it the same way..................

VB.NET
Imports System.ComponentModel

Public Class CollectionWithEvents
    Inherits CollectionBase

#Region " Fields "
     Private _events As EventHandlerList = Nothing

#End Region

#Region " Properties "
     Protected ReadOnly Property Events() As EventHandlerList
        Get
            If _events Is Nothing Then
                _events = New EventHandlerList()
            End If
            Return _events
        End Get
    End Property

#End Region

#Region " Methods "
    Protected Overridable Sub OnThemeChanged(ByVal sender As Object, _
	ByVal e As EventArgs)
        Dim handler As EventHandler = _
	CType(Me.Events("ThemeChangedEvent"), EventHandler)
        If (handler IsNot Nothing) Then
            RaiseEvent ThemeChanged(sender, e)
        End If
    End Sub
#End Region

#Region " Events "
     Public Custom Event ThemeChanged As EventHandler
        AddHandler(ByVal value As EventHandler)
            Me.Events.AddHandler("ThemeChangedEvent", value)
        End AddHandler

        RemoveHandler(ByVal value As EventHandler)
            Me.Events.RemoveHandler("ThemeChangedEvent", value)
        End RemoveHandler

        RaiseEvent(ByVal sender As Object, ByVal e As System.EventArgs)
            CType(Me.Events("ThemeChangedEvent"), EventHandler).Invoke(sender, e)
        End RaiseEvent
    End Event

#End Region

End Class

Using the Code

Download the attached source code which shows how to handle Custom Event……. Enjoy it!

History

  • First release, July 21st 2011

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Engineer
Egypt Egypt
Oil & Gas Engineer
C# & VB.net
Coding For Fun Only

Comments and Discussions

 
QuestionWhy Custom event required. We can call same thing in method in background worker. Pin
Member 1036658317-Apr-16 8:48
Member 1036658317-Apr-16 8:48 
GeneralVery Nice Article Pin
__Jun__15-May-14 3:45
__Jun__15-May-14 3:45 
GeneralRe: Very Nice Article Pin
a_pess15-May-14 4:29
a_pess15-May-14 4:29 
QuestionVery Helpful Pin
scarterszoo6-May-14 8:36
scarterszoo6-May-14 8:36 
GeneralMy vote of 5 Pin
Member 1020462312-Aug-13 4:59
Member 1020462312-Aug-13 4:59 
QuestionIs all this hassle really necessary? Pin
Petoj8725-Jul-13 3:50
Petoj8725-Jul-13 3:50 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey23-Feb-12 21:46
professionalManoj Kumar Choubey23-Feb-12 21:46 

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

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