{
Bei der Klasse TStrings und den Komponenten TTreeview/TListview gibt es die
Möglichkeit, neben dem einfachen einfügen eines Strings auch Objekte mitanzufügen.
Da eine Klasse TObject erwartet wird, muss man einen kleinen Umweg programmieren.
}
{
The Classes TStrings and the components TTreeview/TListview allow you to add an
additional Object to a string.
Since a TObject is expected, you need to make a little detour to achive this.
}
type TMyRecord = record id: Integer;
Name: string; {...} end;
PMyRecord = ^TMyRecord;
{...}
{ In this example I use a Listview component }
procedure Form1.Form1Create(Sender: TObject) var i: Integer;
pRec: PMyRecord; begin
for i := 0 to 10 do
begin new(pRec);
pRec.id := i;
pRec.Name := 'Entry' + IntToStr(i); {...} ListView1.AddItem('Entry' + IntToStr(i), Pointer(pRec)); end; end;
{ To retrieve the stored records just use this: }
procedure Form1.ListView1Click(Sender: TObject); var i: Integer;
xRec: TMyRec; begin
for i := 0 to Listview1.Count - 1 do
if ListView1.Selected[i] then
begin xRec := PMyRecord(ListView1.Items.Objects[i])^;
ShowMessage(Format('Record #%d Name: %s', [xRec.id, xRec.Name])); end; end;
{ finally do not forget to free assigned memory }
procedure Form1.FormClose(Sender: TObject); var i: Integer; begin
for i := 0 to ListView1.Count - 1 do
if ListView1.Items.Objects[i] <> nil then Dispose(ListView1.Items.Objects[i]); end;