unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;type
  TForm1 = class(TForm)
    procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    Count:integer;
    procedure MyClick(Sender:TObject);
  public
    { Public declarations }
  end;var
  Form1: TForm1;implementation{$R *.DFM}procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var Button1:TButton;
begin
  Inc(Count);
  Button1:=TButton.Create(self);
  with Button1 do begin
    Parent:=Self;
    Left:=X;
    Top:=Y;
    Width:=50;
    Height:=20;
    Caption:='Button'+IntToStr(Count);
    OnClick:=MyClick;
    end;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
  Count:=0;
end;procedure TForm1.MyClick(Sender: TObject);
begin
  ShowMessage('Hello!'+(Sender as TButton).Caption);
end;end.

解决方案 »

  1.   

    Tbtn:=Tbutton.create
    tbtn.onclick:=your_procyour_proc
    beginend;
      

  2.   

    OnClick其实是一个属性,你在创建一个新的Button以后,可以给这个属性赋值,不过这个属性的数据类型,是一个(我不知道怎么称呼这种类型)Function of object,实际上是一个指针,指向了一个函数,不过这个函数是特别的,我们知道,对象的方法会默认的传递一个Self的常量,指向拥有这个方法的类的实例,刚才的那个函数指针就是这样特殊的,用她调用函数的时候,会传递Self常量,当然这个常量,不是Button,而是你声明这个方法的时候所在类的实例。有点拗口,希望你能明白。
      

  3.   

    To BlueTrees(蜗牛):
    OnClick 是事件,不是属性。
      

  4.   

    to I_am_zealot(狂战士):昏倒,口吐白沫不省人事。
    事件是一种特殊的属性,你可以看一下事件的声明和属性的声明的差异,你会发现没有差异,区别在于,数据类型不同。
      

  5.   

    ...
    protected
    property OnClick:TNotifyEvent read FOnClick write FOnClick stored IsOnClickStored;
      

  6.   

    look and study from you all
      

  7.   

    设置创建成的按钮的 onclick事件