请问delphi中如何动态的建立对象?比如动态的建立一个BUTTON

解决方案 »

  1.   

    var btn:TButton;
    begin
        btn:=Tbutton.create;
        btn.parent:=self;
        btn.caption:='--------';
        btn.left:=100;
    end;
      

  2.   

    with tbutton.create do
    begin
      parent:=form1;
      left:=50;
      caption:=
      .
      .
    end;
      

  3.   

    如果我事先不知道我要创建多少对象,就是说我要创建多少个button由我的数据库中的数据来决定,这又怎么办,我不能事先申明的呀?
      

  4.   

    声明对象数组:
    var
      CompArr: Array of TButton/TEdit/TLabel;
      

  5.   

    procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    begin
      with TButton.create(self) do
      begin
        SetBounds(X,Y,80,25);
        Parent:=self;
        Caption:=Format('[%d,%d]',[X,Y]);
      end;
    end;
      

  6.   

    更绝的方法:
    with TButton(GetClass('TButton').Create) do
    begin
        SetBounds(X,Y,80,25);
        Parent:=self;
        Caption:=Format('[%d,%d]',[X,Y]);
    end;