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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>C++</strong> Class Frameworks and the Visual Component Model<br />

In this case, you are gett<strong>in</strong>g <strong>in</strong>formation on the button that was pressed, which keyboard keys<br />

were pressed at the time the mouse was clicked, and the x,y coord<strong>in</strong>ate of the cursor when<br />

the mouse button was clicked. The event-handl<strong>in</strong>g function conta<strong>in</strong>s all the <strong>in</strong>formation you<br />

need to deal with the particular event that the event handler is designed to handle.<br />

So what exactly is Sender? Sender is a po<strong>in</strong>ter to the component that is send<strong>in</strong>g the message<br />

to the message handler. In this example, the Sender parameter is extra baggage because we<br />

know that the Show/Hide button is the sender. Sender exists to allow you to have more than<br />

one component use the same event handler. To illustrate, let’s create a new button and have<br />

one of our buttons be the Show button and the other be the Hide button:<br />

1. If the Code Editor is on top, press F12 to switch back to the Form Editor.<br />

2. Click on the Show/Hide button to select it. Change both the Name and Caption<br />

properties to Show.<br />

3. Add a new button to the form to the right of the Show button. Arrange the<br />

buttons, if desired, to give an even look to the form.<br />

4. Change the Name property for the new button to Hide. The Caption property will<br />

also change to Hide (you’ll have to press Enter before the Caption property will<br />

change).<br />

5. Click the Show button and then click on the Events tab <strong>in</strong> the Object Inspector.<br />

Notice that the OnClick event now says ShowClick. Edit it to say ButtonClick aga<strong>in</strong>.<br />

(The <strong>in</strong>itial event handler name is a default name. You can change it to any name<br />

you like.)<br />

6. Click the Hide button and f<strong>in</strong>d the OnClick event <strong>in</strong> the Object Inspector (it<br />

should be selected already). Next to the value is a drop-down arrow button. Click<br />

the arrow button and then choose ButtonClick from the list that drops down (there<br />

should only be one function name <strong>in</strong> the list at this po<strong>in</strong>t).<br />

7. Double-click on the value ButtonClick. You are presented with the Code Editor<br />

with the cursor <strong>in</strong> the ButtonClick() function. Modify the code so that it reads like<br />

this:<br />

void __fastcall TPMEForm::ButtonClick(TObject *Sender)<br />

{<br />

if (Sender == Hide) Memo->Hide();<br />

else Memo->Show();<br />

}<br />

8. Bake at 425 degrees for 1 hour or until golden brown. (Just mak<strong>in</strong>g sure you’re still<br />

awake.)<br />

Right now your form should look similar to Figure 5.4. Compile and run the program. Click<br />

each of the buttons to be sure that they do as advertised.<br />

151<br />

5

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

Saved successfully!

Ooh no, something went wrong!