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

Tagged as

Add Custom Event to a Class in VB.NET

, 21 Jul 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
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.
    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.
    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.
    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.
    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.
    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.
    Public Class ThemeColors
        Implements IDisposable
    
        ' rest of the code goes here
    
    End Class
  • Define a Collection to store the delegates within it.
    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.
    #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..................

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)

Share

About the Author

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

Comments and Discussions

 
GeneralVery Nice Article Pinmember__Jun__15-May-14 4:45 
GeneralRe: Very Nice Article Pinmembera_pess15-May-14 5:29 
QuestionVery Helpful Pinmemberscarterszoo6-May-14 9:36 
GeneralMy vote of 5 PinmemberMember 1020462312-Aug-13 5:59 
QuestionIs all this hassle really necessary? PinmemberPetoj8725-Jul-13 4:50 
GeneralMy vote of 5 Pinmembermanoj kumar choubey23-Feb-12 22:46 

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
Web02 | 2.8.1411023.1 | Last Updated 22 Jul 2011
Article Copyright 2011 by a_pess
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid