怎样动态创建按钮

解决方案 »

  1.   

    var button:TButton;
    begin
       button:=Tbutton.create(self);
       button.parent:=self;
    button.caption:='2222';
    end;
      

  2.   

    var
      mybutton:TButton;
    begin
      mybutton:=TButton.Create(self);
      mybutton.Parent := Form1 ;
      mybutton.Name :='mybutton';
      mybutton.Caption :='mybutton'
      

  3.   

    var
      mybutton:TButton;
    begin
      mybutton:=TButton.Create(self);
      mybutton.Parent := self ;
      mybutton.Name :='mybutton';
      with mybutton do
      begin
      Caption :='mybutton';
      Height:=25;
      left:=260 ;
      .......
      //在这里设置按钮的属性
      end;end;
      

  4.   

    var
      mybutton:TButton;
    begin
      mybutton:=TButton.Create(self);
      mybutton.Parent := self ;//此处SELF代表新建的按钮出在哪个控件上!一般指窗体或面板
      mybutton.Name :='mybutton';
      with mybutton do
      begin
      Caption :='mybutton';
      Height:=25;
      left:=260 ;
      .......
      //在这里设置按钮的属性
      end;end;
      

  5.   

    procedure TForm1.BitBtn1Click(Sender: TObject);
    var
      bb:TButton;
    begin
      bb:=TButton.Create(self);
      bb.Parent := Form1 ;
      bb.Name :='bb';
      bb.Caption :='创建按钮';
      bb.OnClick := myClick;
    end;procedure TForm1.myClick(Sender: TObject);
    begin
       showmessage('Hello');
    end;
    procedure Form1:myClick(Sender: TObject);
    begin
       showmessage('Hello');
    end;
      

  6.   

    以下不是
    procedure Form1:myClick(Sender: TObject);
    begin
       showmessage('Hello');
    end;
      

  7.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, ExtCtrls, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        Panel1: TPanel;
        procedure Button1Click(Sender: TObject);
        procedure btnclick(sender:tobject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
     var
     i:integer;
     j:integer;
     btn:array of tbutton;
    begin
     setlength(btn,100);
     j:=0;
     for i:=0 to 16 do
     begin
       if (i=(5*j)+5) and (i<>0) then j:=j+1;
       btn[i]:=tbutton.Create(self);
       btn[i].Caption :='button' +inttostr(i);
       btn[i].Width:=50;
       btn[i].Height:=50;
       btn[i].Top:=j*(btn[i].Height+10) ;
       btn[i].Left:= (i-5*j)*btn[i].Width+10;
       btn[i].Parent:=self.Panel1 ;
       btn[i].OnClick:=btnclick;
     end;
    end;
    procedure tform1.btnclick(sender:tobject);
    var
    a:string;
    begin
      a:= tbutton(sender).Caption  ;
      application.MessageBox(pchar(a),'提示!');
    end;end.