unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls,stdctrls;type
  TForm1 = class(TForm)
    ControlBar1: TControlBar;
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  Form1: TForm1;implementation{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
var
  newbtn:TButton;
begin
  newbtn := TButton.Create(self);
  newbtn.Caption := 'test';
  newbtn.Parent := self.ControlBar1;
  newbtn.OnClick := Button1Click;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
  showmessage('a');
end;end.

解决方案 »

  1.   

    先定义一个过程,再把按钮的onclick事件指向这个过程就行了。
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, ComCtrls, DB, ADODB;type
      TForm1 = class(TForm)
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
        procedure MyClick(Sender: TObject);
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
    var aa:TButton;
    begin
      aa:=TButton.Create(self);
      aa.Parent:=self;
      aa.Left:=10;
      aa.Top:=10;
      aa.Caption:='ok';
      aa.OnClick:=MyClick;
    end;procedure TForm1.MyClick;
    begin
      ShowMessage('ok');
    end;
    end.
      

  2.   

    对不起,我的意思是:
    b是tcontrol,不是tbutton。
    那么,创建的按钮b,它的onclick 如何实现?
      

  3.   

    首先定义一个过程
    private
      procedure mybuttonclick(sender:tobject);procedure mybuttonclick(sender:tobject);
    begin
      //这里写你点击按钮要做什么
      showmessage('mybutton clicked');
    end;然后在创建新按钮的时候加上
    newbtn.onclick:=mybuttonclick;
    这样就ok了
      

  4.   

    定义一个窗体的类方法,这个类方法有参数sender:tobject.
    然后让onclick=这个类方法。就是楼上的那位写的那样!
      

  5.   

    上面的newbtn:tcontrol,不是newbtn:tbutton;那么newbtn没有onclick事件,请问如何处理?
      

  6.   

    重载他在定义部分加入
    procedure onclick(sender:object);Message WM_CLICK;
    然后实现
      

  7.   

    写的代码如下:
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}
    var
    b:tcontrol;
    procedure TForm1.Button1Click(Sender: TObject);
    begin
       b:=tbutton.Create(self);
       b.Parent:=self;end;end.
    那么,上述按下按钮b的onclick事件如何实现?
      

  8.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
        procedure NewBtnClick(Sender: TObject);
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}
    var
    b:tcontrol;
    procedure TForm1.Button1Click(Sender: TObject);
    begin
       b:=tbutton.Create(self);
       b.Parent:=self;
       (b as TButton).OnClick = NewBtnClick;
    end;
    procedure TForm1.NewBtnClick(Sender: TObject);
    begin
      ...
    end;
    end.