160 Part II Programming Fundamentals
has elapsed on the system clock. In these situations, the computer, not the user, triggers the
important events. But regardless of how an event is triggered, Visual Basic reacts by calling
the event procedure associated with the object that recognized the event and executes
the program code in the event procedure. So far, you’ve dealt primarily with the Click,
CheckedChanged, and SelectedIndexChanged events. However, Visual Basic objects also can
respond to many other types of events.
The event-driven nature of Visual Basic means that most of the computing done in your
programs is accomplished by event procedures. These event-specific blocks of code process
input, calculate new values, display output, and handle other tasks.
In this chapter, you’ll learn how to use decision structures to compare variables, properties,
and values, and how to execute one or more statements based on the results. In Chapter 7,
“Using Loops and Timers,” you’ll use loops to execute a group of statements over and
over until a condition is met or while a specific condition is true. Together, these powerful
flow-control structures will help you build your event procedures so that they can respond
to almost any situation.
Events Supported by Visual Basic Objects
Each object in Visual Basic has a predefined set of events to which it can respond. These
events are listed when you select an object name in the Class Name list box at the top
of the Code Editor and then click the Method Name arrow. (Events are visually identified
in Microsoft Visual Studio by a lightning bolt icon .) You can write an event procedure for
any of these events, and if that event occurs in the program, Visual Basic will execute the
event procedure that’s associated with it. For example, a list box object supports more than
60 events, including Click, DoubleClick, DragDrop, DragOver, GotFocus, KeyDown, KeyPress,
KeyUp, LostFocus, MouseDown, MouseMove, MouseUp, MouseHover, SelectedIndexChanged,
TextChanged, and Validated. You probably won’t need to write code for more than three or
four of these events in your applications, but it’s nice to know that you have so many choices
when you create elements in your interface. The following screen shot shows a partial listing
of the events for a list box object in the Code Editor: