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;
测试成功,给分

解决方案 »

  1.   

    按以下方法创建比较合理var
      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;
      

  2.   

    控件数组应使用全局变量(函数外定义),释放的时候就方便了。释放:for i:=1 to 4 do
    begin
      tmp[i].Free;
      tmp[i]:=nil;
    end;
      

  3.   

    现在已经存在了4个CheckBox,我怎么样才能将他们转换为控件数组来使用。
    我现在是重新生成了控件数组,然后把已经存在的赋值给新生成的控件数组,然后来使用,这种方法我感觉很不合理,请问有没有其他更好的方法?
    谢谢!
      

  4.   

    程序如下:
    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;