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.

286 Day 8<br />

TListBox class, the property that holds the list box items is called Items. For example, do this<br />

exercise:<br />

1. Create a new application and place a ListBox component on the form. Size the list<br />

box as desired.<br />

2. Change the Name property of the list box to ListBox.<br />

3. Double-click on the background of the form (not on the list box). The Code<br />

Editor displays the FormCreate() function.<br />

4. Type the follow<strong>in</strong>g code <strong>in</strong> the FormCreate() function:<br />

char w<strong>in</strong>Dir[256], fileName[256];<br />

GetW<strong>in</strong>dowsDirectory(w<strong>in</strong>Dir, sizeof(w<strong>in</strong>Dir));<br />

spr<strong>in</strong>tf(fileName, “%s\\w<strong>in</strong>.<strong>in</strong>i”, w<strong>in</strong>Dir);<br />

ListBox->Items->LoadFromFile(fileName);<br />

5. Click the Run button to compile and run the program.<br />

When the program runs, the list box will conta<strong>in</strong> the contents of your WIN.INI file. Us<strong>in</strong>g this<br />

method, it’s easy to load a list box from any ASCII text data file. The ComboBox component<br />

also has an Items property, and it works <strong>in</strong> exactly the same way.<br />

You can add, delete, <strong>in</strong>sert, and move items <strong>in</strong> a list box, combo box, or memo by call<strong>in</strong>g the<br />

Add(), Append(), Delete(), Insert(), and Move() methods of the TStr<strong>in</strong>gs class.<br />

NOTE<br />

How Add() performs depends on the value of the Sorted property. If<br />

the Sorted property is set to true, Add() will <strong>in</strong>sert the str<strong>in</strong>g where it<br />

needs to be <strong>in</strong> the list of items. If Sorted is false, the new str<strong>in</strong>g will be<br />

added at the end of the list.<br />

You can clear a component of its contents by call<strong>in</strong>g the Clear() method. You can access an<br />

<strong>in</strong>dividual str<strong>in</strong>g by us<strong>in</strong>g the Str<strong>in</strong>gs property of TStr<strong>in</strong>gs and the array subscript operator.<br />

For example, to retrieve the first str<strong>in</strong>g <strong>in</strong> a list of str<strong>in</strong>gs, you would use<br />

Edit->Text = ListBox->Items->Str<strong>in</strong>gs[0];<br />

Each str<strong>in</strong>g <strong>in</strong> a TStr<strong>in</strong>gs array conta<strong>in</strong>s the str<strong>in</strong>g itself and 4 bytes of extra storage. This extra<br />

storage can be accessed through the Objects property. You can use the extra storage any way<br />

you like. Let’s say, for example, that you were creat<strong>in</strong>g an owner-drawn list box that displayed<br />

bitmaps. You could store the str<strong>in</strong>g <strong>in</strong> the usual way, plus store a po<strong>in</strong>ter to the TBitmap object<br />

<strong>in</strong> the Objects array.

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

Saved successfully!

Ooh no, something went wrong!