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.

150 Day 5<br />

NOTE<br />

Remember when you changed the Name property of the memo component<br />

to Memo? When you did that, <strong>C++</strong>Builder went to work beh<strong>in</strong>d the<br />

scenes. <strong>C++</strong>Builder first derived a class from TMemo. Then it created a<br />

dynamic <strong>in</strong>stance of the new class and gave it the variable name Memo.<br />

In the code <strong>in</strong> this section, Memo is a po<strong>in</strong>ter to the object. That is why<br />

the Show() and Hide() functions are accessed us<strong>in</strong>g the <strong>in</strong>direct member<br />

operator.<br />

That’s all there is to it! But does it work? Let’s f<strong>in</strong>d out. Click the Run button on the speedbar.<br />

After be<strong>in</strong>g compiled, the program runs and is displayed. It’s the moment of truth. Click the<br />

button, and the memo component is hidden. Click the button aga<strong>in</strong>, and the memo<br />

component is aga<strong>in</strong> displayed. It works! Hallelujah! After play<strong>in</strong>g with that for a m<strong>in</strong>ute, close<br />

the program (use the Close Program button <strong>in</strong> the upper-left corner of the title bar) and you<br />

are back to the Code Editor.<br />

Hmmm…all that mess<strong>in</strong>g with the static bool variable is a bit cumbersome. Th<strong>in</strong>k back to<br />

the discussion about properties. Wouldn’t it be nice if the memo component had a property<br />

that could tell us whether the component was currently visible? Is there such a beast? Of<br />

course there is! It’s called, predictably, Visible. Let’s make use of it. Aga<strong>in</strong>, edit the function<br />

until it looks like this:<br />

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

{<br />

if (Memo->Visible) Memo->Hide();<br />

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

}<br />

Aga<strong>in</strong> click the Run button. The program is displayed and, lo and behold, the button does<br />

what it’s supposed to. How about that? We managed to use properties, methods, and events<br />

<strong>in</strong> the same example.<br />

Are you gett<strong>in</strong>g the fever yet? Hold on, because there’s lots more to come. Oh, and wipe that<br />

silly gr<strong>in</strong> off your face…your boss th<strong>in</strong>ks you’re work<strong>in</strong>g!<br />

As you can see, the ButtonClick() function takes a po<strong>in</strong>ter to a TObject called Sender. Every<br />

event-handl<strong>in</strong>g function will have at least a Sender parameter. Depend<strong>in</strong>g on the event be<strong>in</strong>g<br />

handled, the function might have one or more additional parameters. For <strong>in</strong>stance, the<br />

OnMouseDown event handler looks like this:<br />

void __fastcall TPMEForm::ButtonMouseDown(TObject *Sender,<br />

TMouseButton Button, TShiftState Shift, Integer X, Integer Y)<br />

{<br />

}

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

Saved successfully!

Ooh no, something went wrong!