Scalabium Software

SMReport Autogenerated
Knowledge for your independence'.
Home Delphi and C++Builder tips


#94: How can I assign a values to TStrings instead objects?

Sometimes in development you must use the some values (for example, integer or strings) as data to each item in list (TStrings).

For example, you have the listbox with country name but you want to link a code value for each item. Or other example: you have a combobox with colors and you needs to link a color value to each colored line.

These tasks can be easy solved - the TStrings type have:
- the Items property: the string items which will be displayed in combobox or listbox
- the Objects property: the data which can be linked to each visible item.

Each Objects[i] is the pointer to some external data.

In this tip I want to explain how you can use this Objects property for storing of some additional info.

1. to store the integer values:

with qryGroups do
begin
  First;
  while not Eof do
  begin
    cbGroups.Items.AddObject(FieldByName('Name').AsString,
                             TObject(FieldByName('ID').AsInteger));
    Next;
  end;
end;

to read the integer value from selected item:

strID := LongInt(cbGroups.Items.Objects[cbGroups.ItemIndex]);

Comments: in this example, I used the type convertion - each pointer is address. But address is an integer value so we can place to item data the "virtual" address.

 

2. to store the string values:

with qryGroups do
begin
  First;
  while not Eof do
  begin
    cbGroups.Items.AddObject(FieldByName('Name').AsString,
                   TObject(LongInt(NewStr(FieldByName('ID').AsString))));
    Next;
  end;
end;

to read a value from selected item:

strID := PString(cbGroups.Items.Objects[cbGroups.ItemIndex])^;

Comments: in this example I used the address on string which I created (see a help topic on NewStr).

Also don't forget that you must destroy the objects data which you created in this example. For exampe, you can do it in OnDestroy event:

for i := 0 to cbGroups.Items.Count-1 do
  DisposeStr(PString(cbGroups.Items.Objects[i]));


Published: September 11, 2000

See also
 
Database Information Manager
Paradox ActiveX
ExcelFile Viewer
SMReport
Excel Reader (dll)
Fast Document Viewer
ABA Document Convert
ABA Database Convert
Viewer for TNEF-files (winmail.dat)
Paradox Viewer
 
 


Contact to webmaster

 

Borland Software Code Gear Scalabium Delphi tips

Copyright© 1998-2024, Scalabium Software. All rights reserved.
webmaster@scalabium.com

SMExport advertising