12.12.2012 Views

Teach Yourself Borland C++ in 14 Days - portal

Teach Yourself Borland C++ in 14 Days - portal

Teach Yourself Borland C++ in 14 Days - portal

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>14</strong>6 Day 5<br />

Events<br />

W<strong>in</strong>dows is said to be an event-driven environment. Event-driven means that a<br />

program is driven by events that occur with<strong>in</strong> the W<strong>in</strong>dows environment. Events<br />

<strong>in</strong>clude mouse movements, mouse clicks, and keypresses.<br />

Programmers mov<strong>in</strong>g from DOS or ma<strong>in</strong>frame programm<strong>in</strong>g environments may have some<br />

difficulty with the concept of someth<strong>in</strong>g be<strong>in</strong>g event driven. A W<strong>in</strong>dows program cont<strong>in</strong>ually<br />

polls W<strong>in</strong>dows for events. Events <strong>in</strong> W<strong>in</strong>dows <strong>in</strong>clude a menu be<strong>in</strong>g activated, a button<br />

be<strong>in</strong>g clicked, a w<strong>in</strong>dow be<strong>in</strong>g moved, a w<strong>in</strong>dow need<strong>in</strong>g repa<strong>in</strong>t<strong>in</strong>g, a w<strong>in</strong>dow be<strong>in</strong>g<br />

activated, and so forth. W<strong>in</strong>dows notifies a program of an event by send<strong>in</strong>g a W<strong>in</strong>dows<br />

message. There are somewhere <strong>in</strong> the neighborhood of 175 possible messages that W<strong>in</strong>dows<br />

can send to an application. That’s a lot of messages! Fortunately, you don’t have to know<br />

about each and every one of them to program <strong>in</strong> <strong>C++</strong>Builder; there are only a couple dozen<br />

that are used frequently.<br />

In VCL, an event is anyth<strong>in</strong>g that occurs <strong>in</strong> the component that the user might need to know<br />

about. Each component is designed to respond to certa<strong>in</strong> events. Usually this means a<br />

W<strong>in</strong>dows event, but it can mean other th<strong>in</strong>gs as well. For example, a button component is<br />

designed to respond to a mouse click, as you would expect. But a nonvisual control such as<br />

a database component might respond to non-W<strong>in</strong>dows events such as the user reach<strong>in</strong>g the<br />

end of the table.<br />

When you respond to a component’s event, you are said to handle the event.<br />

Events are handled through functions called event handlers.<br />

NEW TERM<br />

NEW TERM<br />

NOTE<br />

VCL makes it <strong>in</strong>credibly easy to handle events. The events that a component has been<br />

designed to handle are listed under the Events tab <strong>in</strong> the Object Inspector w<strong>in</strong>dow. An event<br />

name is descriptive of the event to which it responds. For <strong>in</strong>stance, the event to handle a<br />

mouse click is called OnClick.<br />

You don’t have to handle every event that a component def<strong>in</strong>es. In fact,<br />

you rarely do. If you do not respond to a particular event, the event<br />

message is either discarded or handled <strong>in</strong> a default manner, as described<br />

by either VCL or the component itself. You can handle any events you<br />

have an <strong>in</strong>terest <strong>in</strong> and ignore the rest.<br />

This will make more sense if you can put it <strong>in</strong>to practice. To beg<strong>in</strong>, let’s start a new application<br />

from scratch. Choose File | New Application from the ma<strong>in</strong> menu. If you are prompted to<br />

save the current project, click No. Now you should aga<strong>in</strong> have a blank form. First, let’s set<br />

up the ma<strong>in</strong> form:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!