如何动态创建按钮?以及如何让其响应click事件!

解决方案 »

  1.   

    sbutton:= TButton.Create(self);
    sbutton.onclick:= ....
    这种问题很多拉
    搜索一下先吧
      

  2.   

    procedure TForm1.ButtonClick(Sender: TObject);
    begin
      showmessage('This Button caption is ' + TButton(Sender).Caption);
    end;procedure TForm1.Button1Click(Sender: TObject);
    var
      AButton: TButton;
      i: Integer;
    begin
      for i := 1 to 5 do
      begin
        AButton := TButton.Create(self);
        AButton.Parent := self;
        AButton.Top := i*100;
        AButton.Caption := IntToStr(i*100);
        AButton.OnClick := ButtonClick;
      end;end;
      

  3.   

    z_hongbao() 的可以,如果多的话就要用变量控制顶和左!宽和高也指出吧!还有parent and visible!
      

  4.   

    procedure TForm1.Button1Click(Sender:TObject);
    var
    b:TButton;
    begin
     B:=TButton.Create(self);
     B.Parent:=self;
     B.left:=100;
     B.Top:=100;
     B.Caption:='Button2';
    // B.OnClick:=……
    end;
      

  5.   

    to:z_hongbao() 
    谢谢你,你的代码有一处不明
    abutton.parent=self;
    这个self指谁?form 还是什么?????
      

  6.   

    如果你的BUTTON在PANEL1
    BUTTON.Parent:=Panel1;
      

  7.   

    to smallxu(咖啡) 
    self指當前Form1
      

  8.   

    如果是多个按扭的话,可以用tag属性控制~!
      

  9.   

    给你个例子
    例如:
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;type
      TForm1 = class(TForm)
           procedure FormCreate(Sender: TObject);
           procedure aaa(sender:tobject);//过程
      private
        { Private declarations }
      public
        { Public declarations }
      end;
    var
      Form1: TForm1;
      btn:tbutton;implementation{$R *.DFM}
    procedure tform1.aaa(sender:tobject);
    begin
         showmessage('aaaaaaaaaa');
    end;procedure TForm1.FormCreate(Sender: TObject);
    begin
        btn:=tbutton.Create(self);
        btn.Parent:=form1;
        btn.Visible:=true;
        btn.onClick:=aaa;//赋过程名
    end;
    procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
    begin
         btn.Free;
    end;
    end.