Читать книгу Beginning Programming All-in-One For Dummies - Wallace Wang - Страница 41
Writing event handlers
ОглавлениеThe whole purpose of an event handler is to work as a middleman between your actual program and your program’s UI. To create an event handler, you need to identify the following:
A UI item, such as a button or a check box
The event to respond to, such as a click of the mouse
An event handler responds to a certain event triggered by a specific UI item, as shown in Figure 2-7. A UI item can have one or more event handlers so it can respond to different types of events, such as the user clicking a mouse button or pressing a key. A single event handler can respond to the same event coming from different UI items such as three different buttons.
FIGURE 2-7: An event handler tells the UI how to behave when the user does something, such as click the mouse over a button.
The user can do dozens of different possible events, but some common events are clicking the mouse or pressing a key. Event handlers typically do one of three things:
Identify what the user did, such as click a button
Retrieve information from the UI, such as when the user types something in a text box
Display information to the user, such as an error message
After you’ve written one or more event handlers for your UI, you have a complete working UI. Now you just have to attach this UI to a working program.