我动态创建了一个对象,怎么为他动态创建一个时间
例如:
 button1:=tbuuton1.create(self);
 怎么给button1创建onclick事件?如果不需要,怎么直接使用这个事件?代码怎么写?

解决方案 »

  1.   

    给你个例子,看了你就明白了:unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        procedure FormCreate(Sender: TObject);
      private
        MyButton1 : TButton;
        procedure AButtonClick(Sender: TObject);
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
    begin
      MyButton1 := TButton.Create(Self);
      with MyButton1 do
      begin
        Name := 'MyButton1';
        Left := 10;
        Top := 10;
        Width := 120;
        Height := 32;
        Caption := '测试';
        Parent := Self;
        OnClick := AButtonClick;
      end;
    end;procedure TForm1.AButtonClick(Sender: TObject);
    begin
      ShowMessage (TButton(Sender).Name + ' be clicked.');
    end;end.