1. Technology

Creating Your Own Event Code

It's not so hard. But it is precise!


Coding your own event procedures is an unfamiliar technique for many VB programmers. We all use them constantly when they're provided by components, but we don't code them very often. They're actually not too hard to program, but you do have to get things exactly right.

Event procedures work because every Windows application is based on a program loop that is called the event loop or message pump. Windows inserts messages, information packed objects consisting of a 16-bit numeric code along with a data payload of a pair of 32-bit numeric values, into a Message Queue when a wide variety of things happen such as mouse clicks or keyboard actions. Your application processes the queue and passes the information along to a subroutine that you code for specific types of messages.

A typical example would be the Click event subroutine for a Button component.

When you click the mouse, the operating system adds a message to the queue (actually, more than one message is added to the queue) and the code provided by the .NET Framework calls a subroutine you code that looks something like this:

Private Sub myButton_Click( _
   ByVal sender As System.Object, _
   ByVal e As System.EventArgs) _
   Handles myButton.Click
   ' Your code goes here
End Sub

You can go way beyond this by creating your own events and then coding subroutines that respond to those events. A typical example that you might find in a book might be the event of an employee getting a raise in pay. The SalaryChange event is raised by the employee object. To make sure I had the concepts firmly in mind, I coded my own example based on my own familiar experiences. Since I don't remember getting a salary raise that much, I decide to recreate a drinking binge on Saturday night in code instead.

Click Here to display the illustration
Click the Back button on your browser to return

The first thing to code is an object to represent a drinking binge:

Public Class Binge

' the class code goes here

End Class

The most important property of a drinking binge is how much drink you have left in your glass.

Public Class Binge
   Private glassContains As Decimal
   Public Event EmptyGlass()

' more class code goes here

End Class

Of course, only you care whether you have anything left in your glass, so that information is private. But when your glass is empty, you need to involve the bartender and a friend you brought along to pay for the drinks so it's a public event.

Every good binge starts with a first drink.

Public Sub New()
   glassContains = 1
End Sub

The fundamental method of a drinking binge is drinking and the main information is how much of your drink you consume with each drink. This eventually results in your glass being empty so that's the data item that is calculated with each drink. When you finally empty your glass, the object raises the event EmptyGlass that was declared.

Public Sub Drink(ByVal thisDrink As Decimal)
   Dim glassIsEmpty As Boolean = False
   If glassContains < thisDrink Then
      thisDrink = glassContains
      glassIsEmpty = True
      glassContains -= thisDrink
   End If
   MsgBox("I just drank " & thisDrink.ToString _
      & " of my drink")
   If glassIsEmpty Then
      RaiseEvent EmptyGlass()
   End If
End Sub

The main method that makes this object a drinking binge is the HaveAnother method.

Public Sub HaveAnother()
   MsgBox("My glass is empty!" & vbCrLf & _
   "A man has got to believe something ... " & vbCrLf & _
   "I believe I'll have another drink!")
   glassContains = 1
End Sub

These are the essential components of a good drinking binge. Notice that there are just two statements that relate directly to creating your own event:

Public Event EmptyGlass()
RaiseEvent EmptyGlass()

The first creates a message that can be inserted into the message queue. The second actually inserts it. Processing that message, just like the code you have been writing all along, is a job for your main program.

The first job your main program has to do is instantiate the Binge object. The Binge object is just a general model. A real drinking binge is a specific night. But since we are planning ahead, we tell the compiler that an event will have to be processed for this object.

Friend WithEvents SaturdayNight As New Binge

Just as a comparison, if you add, for example, a Button component to a form, you will find this statement in the hidden Designer code:

Friend WithEvents Button1 As System.Windows.Forms.Button

  1. About.com
  2. Technology
  3. Visual Basic
  4. Learn VB.NET
  5. Creating Your Own Event Code

©2014 About.com. All rights reserved.