unit ListViewTest;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls;
type
PMyRec=^TMyRec;
TMyRec=record
Text1,
Text2 :
String;
Zahl1,
Zahl2 :Integer;
end;
// TMyRec
TMyRecList=class(TList)
private
function GetDaten(aIndex:Integer):PMyRec;
procedure SetDaten(aIndex:Integer; aRec:PMyRec);
public
procedure AddRec(
const aRec:TMyRec);
procedure Clear;
override;
property Daten[aIndex:Integer]:PMyRec
read GetDaten
write SetDaten;
end;
// TMyRecList
TForm1 =
class(TForm)
ListView1: TListView;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure ListView1Data(Sender: TObject; Item: TListItem);
private
{ Private-Deklarationen }
fMyRecList:TMyRecList;
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
// ----- TMyRecList
procedure TMyRecList.AddRec(
const aRec:TMyRec);
var r:PMyRec;
begin
r:=AllocMem(SizeOf(TMyRec));
r^:=aRec;
Add(r);
end;
// TMyRecList.AddRec
procedure TMyRecList.Clear;
var r:PMyRec;
i:Integer;
begin
for i:=0
to Count-1
do
begin
r:=Daten[i];
if (r<>
nil)
then FreeMem(r, SizeOf(TMyRec));
end;
// for i
inherited Clear;
end;
// TMyRecList.Clear
function TMyRecList.GetDaten(aIndex:Integer):PMyRec;
begin
if ((aIndex>=0)
and (aIndex<Count))
then Result:=PMyRec(Items[aIndex])
else Result:=nil;
end;
// TMyRecList.GetDaten
procedure TMyRecList.SetDaten(aIndex:Integer; aRec:PMyRec);
begin
if ((aIndex>=0)
and (aIndex<Count))
then PMyRec(Items[aIndex])^:=aRec^;
end;
// TMyRecList.SetDaten
// ----- TForm1
procedure TForm1.FormCreate(Sender: TObject);
var i:Integer;
r:TMyRec;
begin
fMyRecList:=TMyRecList.Create;
for i:=1
to 2000
do
begin
with r
do
begin
Text1:=Format('
Text %d, Spalte 1', [i]);
Text2:=Format('
Text %d, Spalte 2', [i]);
Zahl1:=i;
Zahl2:=i+1;
end;
// with
fMyRecList.AddRec(r);
end;
// for i
// der Listview mitteilen, wieviele Einträge existieren
ListView1.Items.Count:=fMyRecList.Count;
ListView1.Repaint;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
fMyRecList.Free;
end;
procedure TForm1.ListView1Data(Sender: TObject; Item: TListItem);
var r:PMyRec;
begin
if (Item=nil)
then Exit;
r:=fMyRecList.Daten[Item.
Index];
if (r<>
nil)
then with Item
do
begin
Caption:=r^.Text1;
SubItems.Add(r^.Text2);
SubItems.Add(IntToStr(r^.Zahl1));
SubItems.Add(IntToStr(r^.Zahl2));
end;
// if
end;
end.