1. Technology

Events - Program and Handle Your Own Unique Event in VB.NET

Handling an event is easy. What about creating one in your own code?

By

'Happy, middle-aged woman using a laptop'
Yuri/Vetta/Getty Images

A reader wanted to know how to create and handle an event.

"My project contains a class 'myClass'. I'd like to 'call/throw' an event to the program that has instantiated myClass."

No problem!

First, "MyClass" is a reserved keyword. Microsoft's documentation states that it, "Provides a way to refer to the current class instance members without them being replaced by any derived class overrides." So you couldn't have named it that ... but we'll overlook that and call it myEventClass.

But the second problem is that you have to have an "event" in your class to raise. I decided to simply use the Tick event of a Timer component every 5 seconds to trigger the internal event just to demonstrate how to do this. The "event" could be raised by a FileSystemWatcher, an error condition, or something else.

Here's the code to create the event (along with the Timer code):

 Public Class myEventClass
    Dim WithEvents myTimer As New Timer
    Public Sub New()
       myTimer.Enabled = True
       myTimer.Interval = 5000
    End Sub
    Public Event TimesUp()
    Private Sub myTimer_Tick( _
       ByVal sender As System.Object, _
       ByVal e As System.EventArgs) _
       Handles myTimer.Tick
       RaiseEvent TimesUp()
    End Sub
 End Class 

Since this is a bare class with no VB.NET generated code to help it out, I have to declare the Timer component as "WithEvents". Just to fill out your understanding of how things work, normally, the generated "Designer.vb" code contains this declaration. Here's the way VB.NET does it for a regular form:

 Me.myTimer = New System.Windows.Forms.Timer(Me.components)
 ...
 Friend WithEvents myTimer As System.Windows.Forms.Timer 

WithEvents tells VB.NET that an instance of this class can raise events. VB.NET doesn't generate the code necessary if you don't add this keyword. You also need to declare the Event and the statement "Public Event TimesUp()" does that. Finally, you have to raise the event. The statement "RaiseEvent TimesUp()" does that. An object that can raise an event is called an "event sender" or sometimes an "event source".

When this class is instantiated, you have to use the WithEvents keyword again with this class since it's an event sender. The code to handle the event is a lot like the code that VB.NET generates:

    Dim WithEvents myNewClass _
       As New myEventClass
    Private Sub handleTimesUp() _
       Handles myNewClass.TimesUp
       Console.WriteLine( _
       "TimesUp Event Raised and Handled")
    End Sub 

All the tricks you have used so far ... such as AddHandler, RemoveHandler ... will also work with your new event.

  1. About.com
  2. Technology
  3. Visual Basic
  4. Quick Tips
  5. Creating and Handling an Event in Your Own VB.NET Class

©2014 About.com. All rights reserved.