DELPHI中如何实现控件数组(100分)

  • 主题发起人 主题发起人 snny
  • 开始时间 开始时间
S

snny

Unregistered / Unconfirmed
GUEST, unregistred user!
[8D]DELPHI中如何实现控件数组,好像有一种TLIST方式,不知具体如何操作
 
http://go8.163.com/windstorm2000/tips.htm#14.在程序中动态生成控件数组
 
一个生成5个edit控件的例子:
var
Form1: TForm1;
x:array of tedit;
n:Integer;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
SetLength(x,5);
for n:=0 to 4 do
begin
x[n]:=tedit.Create(self);
x[n].Parent:=Form1;
x[n].Left:=10;
x[n].Top:=n*30+10;
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
for n:=0 to 4 do
x[n].Text:=IntToStr(n*2+10);
end;
 
var mylist:tlist;
mybutton:tbutton;
temp:integer;
begin
if mylist<>nil then
begin
mybutton:tbutton.create(self);
mybutton.parent:=self;
temp:=mylist.add(mybutton);
with mybutton do
begin
top:=temp*25;
left:=0;
caption:=inttostr(temp);
onclick:=buttonindexofclick;
end;
end;

delete :
var temp:tbutton
if mylist<>nil then
if mylist.count>0 then
begin
temp:=mylist.items[0];
temp.free;
mylist.delete(0);
end;

 
type
PMyList = ^AList;
AList = record
I: Integer;
C: Char;
end;

var

MyList: TList;
ARecord: PMyList;
B: Byte;
Y: Word;
begin
MyList := TList.Create;
try
New(ARecord);
ARecord^.I := 100;
ARecord^.C := 'Z';
MyList.Add(ARecord); {Add integer 100 and character Z to list}
New(ARecord);
ARecord^.I := 200;
ARecord^.C := 'X';
MyList.Add(ARecord); {Add integer 200 and character X to list}

{ Now paint the items onto the paintbox}
Y := 10; {Variable used in TextOut function}

for B := 0 to (MyList.Count - 1) do
begin
ARecord := MyList.Items;
Canvas.TextOut(10, Y, IntToStr(ARecord^.I)); {Display I}
Y := Y + 30; {Increment Y Value again}
Canvas.TextOut(10, Y, ARecord^.C); {Display C}
Y := Y + 30; {Increment Y Value}
end;

{ Cleanup: must free the list items as well as the list }
for B := 0 to (MyList.Count - 1) do
begin

ARecord := MyList.Items;
Dispose(ARecord);
end;
finally
MyList.Free;
end;
 
后退
顶部