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.

478 Day 13<br />

Giv<strong>in</strong>g the New Form Life<br />

When your form is all set up visually, you still need to attach code to its events <strong>in</strong> order for<br />

it to do anyth<strong>in</strong>g. The biggest event that you’ll customize is the OnBeg<strong>in</strong>Retrieval event of<br />

the THTML component. Of course, you’ll also attach custom code to the OnClick event of both<br />

your buttons and also to the OnChange event of your TTabControl.<br />

Beg<strong>in</strong> by click<strong>in</strong>g on your TTabControl and then double-click<strong>in</strong>g its OnChange event <strong>in</strong> the<br />

Object Inspector. Type the follow<strong>in</strong>g code <strong>in</strong>to the <strong>C++</strong>Builder Code Editor:<br />

NOTE<br />

cbURL->Text=tcURL->Tabs->Str<strong>in</strong>gs[tcURL->TabIndex];<br />

cbURL->SelectAll();<br />

bbGo->Click();<br />

This code sample and the others <strong>in</strong> this chapter assume you’ve adopted<br />

some sort of nam<strong>in</strong>g conventions for the objects <strong>in</strong> your application. I<br />

always prefix identifier names with a two-character mnemonic that<br />

allows me to identify the type of component represented by the<br />

identifier. Here, cbURL refers to the TComboBox control you placed on the<br />

bottom TPanel, tcURL refers to the TTabControl at the top of the form,<br />

and bbGo refers to the Go! TBitBtn component.<br />

Sett<strong>in</strong>g cbURL’s Text property to the text of the tab that was clicked when the OnChange event<br />

fired ensures that the cbURL control and the tcURL control stay synchronized. Call<strong>in</strong>g the<br />

SelectAll() method ensures that the TComboBox’s Text property value is highlighted. The call<br />

to bbGo’s Click() event simulates actually click<strong>in</strong>g the button. This allows click<strong>in</strong>g a tab and<br />

click<strong>in</strong>g the Go! button to behave identically.<br />

Now that the tab control is set up, click on the THTML component and then double-click on<br />

its OnBeg<strong>in</strong>Retrieve event <strong>in</strong> the <strong>C++</strong>Builder Object Inspector. Type this code <strong>in</strong>to the Code<br />

Editor:<br />

Cursor=(TCursor)crHourGlass;<br />

if (cbURL->Items->IndexOf(htBrowser->URL)==-1) {<br />

cbURL->Items->Add(htBrowser->URL);<br />

cbURL->Text=htBrowser->URL;<br />

cbURL->SelectAll();<br />

tcURL->Tabs->Add(htBrowser->URL);<br />

tcURL->TabIndex=tcURL->Tabs->Count-1;<br />

bbGo->Click();<br />

}<br />

This code checks whether the list of URLs ma<strong>in</strong>ta<strong>in</strong>ed by the TComboBox conta<strong>in</strong>s the one<br />

about to be browsed. If the URL isn’t there, the code adds it to both the TComboBox and the<br />

TTabControl.

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

Saved successfully!

Ooh no, something went wrong!