Chapter 4. Handling Events

An event is something that a control raises to tell the program that something significant has happened. Events are extremely important because they are the main way the user controls the program. When the user clicks buttons, drags sliders, and selects menu items, events tell the program that something has happened so it can take action.

As Lesson 2 briefly explained, when the user clicks a Button, the Button raises a Click event. An event handler can catch or handle that event and take appropriate action such as displaying a message, performing a calculation, or downloading the latest Dilbert comic from the Web.

Click is a very useful event, but it's only one of hundreds (if not thousands) of events that your programs can catch.

This lesson explains how you can catch events other than Click. It describes some of the most useful events provided by common controls and, as a bonus, explains how you can display messages to the user when events occur.


The easiest way to build an event handler is to double-click a control in the Form Designer. This creates an empty event handler for the control's default event and opens the event handler in the Code Editor. You then insert the code needed to take whatever action is appropriate.

The following code shows the empty Click event handler created for a Button. (Visual Studio puts the first two lines on a single line in the Code Editor. I split them so they would fit the book's dimensions.)

Private ...

Get Stephens' Visual Basic® Programming 24-Hour Trainer now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.