Author: Tomas Rutkauskas
In my application I have a TListBox control and I need to store additional data
along with each item it contains. The additional data will be an integer variable
and a status variable. What is the best way of doing this? Is there some other
control I can use?
Answer:
TListBox objects are pointers, but you can cast a 32-bit integer to a pointer and
store its value directly. Therefore, if your "integer variable" and "status
variable" can be crammed into 32 bits, you can do this:
1 2 procedure TForm1.Button1Click(Sender: TObject);
3 var4 MyInt, MyStatus: smallint;
5 Combine: integer;
6 begin7 MyInt := 1234;
8 MyStatus := 5678;
9 Combine := MyInt or (MyStatus shl 16);
10 with ListBox1.Items do11 begin12 { store the string and data }13 AddObject('Foobar', pointer(Combine));
14 { retrieve the data }15 MyInt := integer(Objects[0]) and $FFFF;
16 MyStatus := integer(Objects[0]) shr 16;
17 end;
18 end;