这是我刚刚写的一段代码,应该能满足你的功能 VAR Form1 : TForm1; AllControl : Tlist;IMPLEMENTATION{$R *.DFM}PROCEDURE TForm1.FormCreate(Sender: TObject); BEGIN AllControl :=Tlist.create(); END;PROCEDURE TForm1.FormDestroy(Sender: TObject); BEGIN AllControl.free(); END;PROCEDURE TForm1.BitBtn1Click(Sender: TObject); VAR index : integer; BEGIN index := Allcontrol.Add(tButton.Create(self)); WITH Tbutton(AllControl.Items[index]) DO BEGIN caption := Format('测试%d', [index]); parent := self; left := 0; top := 50 * index + 10; width := 50; height := 50; tag := index; onClick := self.ButtonClick; //事件处理 visible := true; END;END;PROCEDURE TForm1.ButtonClick(Sender: TObject); BEGIN showmessage(Format('你点的是%d', [(sender AS Tbutton).tag])) END;END.
var
aaa:myclass
begin
aaa:=myclass.create;
try
...........
finally
aaa.free;
end;
end;
或者
with myclass.create do
begin
try
...........
finally
aaa.free;
end;
end;
比如实例的个数是不确定的
var中就无法声明
有没有直接分配内存然后创建对象
想创建多少就多少
可以存储对象吗
给个详细的例子好吗?
VAR
Form1 : TForm1;
AllControl : Tlist;IMPLEMENTATION{$R *.DFM}PROCEDURE TForm1.FormCreate(Sender: TObject);
BEGIN
AllControl :=Tlist.create();
END;PROCEDURE TForm1.FormDestroy(Sender: TObject);
BEGIN
AllControl.free();
END;PROCEDURE TForm1.BitBtn1Click(Sender: TObject);
VAR
index : integer;
BEGIN
index := Allcontrol.Add(tButton.Create(self));
WITH Tbutton(AllControl.Items[index]) DO
BEGIN
caption := Format('测试%d', [index]);
parent := self;
left := 0;
top := 50 * index + 10;
width := 50;
height := 50;
tag := index;
onClick := self.ButtonClick; //事件处理
visible := true;
END;END;PROCEDURE TForm1.ButtonClick(Sender: TObject);
BEGIN
showmessage(Format('你点的是%d', [(sender AS Tbutton).tag]))
END;END.