procedure TForm1.Button1Click(Sender: TObject);
var
i:integer;
tmp:array[1..4] of TCheckBox;
begin
for i:=1 to 4 do
begin
tmp[i]:=TCheckBox.Create(self);
tmp[i].Parent:=GroupBox1;
tmp[i].Caption :='checkbox'+inttostr(i);
tmp[i].Top:=i*20+20;
tmp[i].Left:=5;
tmp[i].Show;
end;
end;
测试成功,给分
var
i:integer;
tmp:array[1..4] of TCheckBox;
begin
for i:=1 to 4 do
begin
tmp[i]:=TCheckBox.Create(self);
tmp[i].Parent:=GroupBox1;
tmp[i].Caption :='checkbox'+inttostr(i);
tmp[i].Top:=i*20+20;
tmp[i].Left:=5;
tmp[i].Show;
end;
end;
测试成功,给分
Index:integer;
Inc(Index);
with TCheckBox.Create(self) do
begin
Parent:=GroupBox1;
Caption :='CheckBox'+IntToStr(Index);
Left:=10;
Top:=Index*20;
end;最后使用下面办法释放对象
var
I:integer;
begin
for i:=Form1.ComponentCount-1 downto 0 do
begin
if Form1.Components[i].ClassName ='TCheckBox' then
Form1.Components[i].Free;
end;
begin
tmp[i].Free;
tmp[i]:=nil;
end;
我现在是重新生成了控件数组,然后把已经存在的赋值给新生成的控件数组,然后来使用,这种方法我感觉很不合理,请问有没有其他更好的方法?
谢谢!
var
tmp:array[1..4] of TCheckBox;
...
...
var
i:integer;
begin
for i:=1 to 4 do
begin
tmp[i]:=TCheckBox.Create(self);
tmp[i].Parent:=form1;
tmp[i].Top:=RadioGroup1.Top+i*30+5;
tmp[i].Left:=RadioGroup1.Left+5;
tmp[i].Caption:='MyCheckBox'+IntToStr(i);
tmp[i].Hide;
end;
tmp[1]:=CheckBox1;
tmp[2]:=CheckBox2;
tmp[3]:=CheckBox3;
tmp[4]:=CheckBox4;
end;