我在程序中动态创建了几个Button,哪位大虾知道,怎样为它们创建Click事件处理函数,我想实现在Click事件中显示Button的name,多谢.

解决方案 »

  1.   

    procedure TForm1.Button1Click(Sender: TObject);
    begin
        ShowMessage((Sender as TButton).Name);
    end;
    //btn动态创建的按钮
    btn.OnClick := Button1Click;
      

  2.   

    procedure TForm1.ButtonClick(Sender: TObject);
    var
      MyLabel: TLabel;
      MyButton: TButton;
    begin
      Inc(Count);
      MyButton:= TButton.Create(self);
      MyButton.Parent:= Form1;
      MyButton.Left:= (Count-1)* 80;
      MyButton.Top:= (Count-1)* 20;
      MyButton.Name:= 'Button'+IntToStr(Count);
      MyButton.Caption:= 'Button'+IntToStr(Count);
      MyButton.OnClick:= MyButtonClick;
    end;procedure TForm1.MyButtonClick(Sender: TObject);
    begin
      showmessage(TButton(Sender).Name);
    end;