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
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
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
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