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.

Creat<strong>in</strong>g Applications <strong>in</strong> <strong>C++</strong>Builder<br />

Add<strong>in</strong>g a Function to Your Code<br />

In order to illustrate add<strong>in</strong>g a function to an application, let’s implement h<strong>in</strong>t text for the<br />

ScratchPad program you wrote earlier. First, reopen the ScratchPad program. If you skipped<br />

your homework assignment for Day 7, get ScratchPad from http://www.mcp.com/sams/<br />

codecenter.html.<br />

First, we need to prepare the way. We need to assign h<strong>in</strong>t text to each of the speed buttons<br />

and prepare the status bar to receive the h<strong>in</strong>ts. Do the follow<strong>in</strong>g:<br />

1. Be sure the ScratchPad ma<strong>in</strong> form is visible. Click the File Open speed button on<br />

the ma<strong>in</strong> form’s speedbar.<br />

2. Locate the H<strong>in</strong>t property <strong>in</strong> the Object Inspector and type the follow<strong>in</strong>g for the<br />

h<strong>in</strong>t text:<br />

New|Create a New File<br />

3. Change the ShowH<strong>in</strong>t property to true.<br />

4. Repeat steps 2 and 3 for all buttons on the speedbar, add<strong>in</strong>g appropriate h<strong>in</strong>t text<br />

for each type of button.<br />

5. Click on the status bar component along the bottom of the ma<strong>in</strong> form. Change the<br />

SimplePanel property to true. This will allow the full status bar to display a text<br />

str<strong>in</strong>g through the SimpleText property.<br />

Okay, now we have everyth<strong>in</strong>g ready to go, so it’s time we did what you came here for. We’re<br />

go<strong>in</strong>g to create our own OnH<strong>in</strong>t handler and, not surpris<strong>in</strong>gly, we’re go<strong>in</strong>g to name the<br />

function OnH<strong>in</strong>t(). Let’s take this one step at a time. First, we’ll add the function declaration<br />

to the class declaration. Here goes:<br />

1. Switch to the Code Editor and click on the SPMa<strong>in</strong>.cpp tab.<br />

2. Right-click on the Code Editor w<strong>in</strong>dow and choose Swap Cpp/Hdr Files from the<br />

speed menu. The SPMa<strong>in</strong>.h tab appears next to the SPMa<strong>in</strong>.cpp tab and becomes the<br />

active code w<strong>in</strong>dow.<br />

3. Scroll down through the class declaration for the TScratchPad class until you locate<br />

the private section. Add this l<strong>in</strong>e of code after the private keyword:<br />

void __fastcall OnH<strong>in</strong>t(TObject* Sender);<br />

To give you perspective, the last few l<strong>in</strong>es of the class declaration should now look<br />

like this:<br />

void __fastcall FormCreate(TObject *Sender);<br />

private: // User declarations<br />

void __fastcall OnH<strong>in</strong>t(TObject* Sender);<br />

public: // User declarations<br />

virtual __fastcall TScratchPad(TComponent* Owner);<br />

};<br />

339<br />

9

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

Saved successfully!

Ooh no, something went wrong!