Event-Driven Programming


Event-Driven Programming

The programs you've written so far in this book have displayed Toolbox controls, menus, toolbars, and dialog boxes on the screen, and with these programs, users could manipulate the screen elements in whatever order they saw fit. The programs put the user in charge, waited patiently for a response, and then processed the input predictably. In programming circles, this methodology is known as event-driven programming. You build a program by creating a group of “intelligent” objects that know how to respond when the user interacts with them, and then the program processes the input by using event procedures associated with the objects. The following diagram shows how an event-driven program works in Visual Basic:

graphic

Program input can also come from the computer system itself. For example, your program might be notified when a piece of electronic mail arrives or when a specified period of time has elapsed on the system clock. The computer, not the user, triggers these events. Regardless of how an event is triggered, Visual Basic reacts by calling the event procedure associated with the object that recognized the event. So far, you've dealt primarily with the Click, CheckedChanged, and SelectedIndexChanged events. However, Visual Basic objects also can respond to several 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 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, 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 illustration shows a partial listing of the events for a list box object in the Code Editor:

graphic



Microsoft Visual Basic 2005 Step by Step
Microsoft Visual Basic 2005 Step by Step (Step by Step (Microsoft))
ISBN: B003E7EV06
EAN: N/A
Year: 2003
Pages: 168

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net