O'Reilly logo

Learning Visual Basic .NET by Jesse Liberty

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Events

When something happens in a program, we call that occurrence an event. Clicking on a button is an event. Closing a window, dropping down a listbox, pressing a key, and moving the mouse are all events. Windows is an event-driven operating system.

How does the operating system know when you've pressed a key? There are, in general, two ways for the system to receive notification. One way is called polling. In this approach, the operating system asks the keyboard: "Hey! Do you have a keypress for me? Nope? Okay, I'll check again in few milliseconds." This is very much the same as the way I find out if I have mail. Every few milliseconds, I walk down to the mailbox and look inside. Sometimes there is mail; most of the time it is empty.

The second, much preferable, way to keep the operating system apprised of events is to ask the keyboard to notify the operating system when a key is pressed. This is analogous to how the telephone works. You don't pick up the phone every few minutes to see if you have a call. Instead, you leave the phone alone, and when there is a call, the phone notifies you (typically, just as you sit down to dinner).

This act of notifying you is called raising an event. In this case, the event is "a call is ready for you."

VB.NET provides extensive support for handling events such as button clicks. The Button object is declared with the keyword WithEvents. Right-click on your form and choose the View Code item from the pop-up menu. VS.NET will display the ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required