| You are here: | About>Computing & Technology>Visual Basic> Learn VB.NET> Creating Your Own Event Code |
![]() | Visual Basic |
Creating Your Own Event CodeIt'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( _ 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. -------- 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 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() 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) The main method that makes this object a drinking binge is the HaveAnother method. Public Sub HaveAnother() 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() 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 |
Las Vegas on a BudgetFind a BargainHotel DealsCheap EatsFree AttractionsEntertainment for Less |
All Topics | Email Article | | | ![]() |
| Advertising Info | News & Events | Work at About | SiteMap | Reprints | Help | Our Story | Be a Guide |
| User Agreement | Ethics Policy | Patent Info. | Privacy Policy | ©2008 About, Inc., A part of The New York Times Company. All rights reserved. |


