
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 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.
MAKING EVENT HANDLERS
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 would then insert the code needed to take whatever action
is appropriate.
The following code shows the empty
Click event handler created for a Button:
private void crashSystemButton_Click(object sender, ...