如题,谢谢

解决方案 »

  1.   

    Onclick事件里面能够捕捉到Button的tag;
      

  2.   

    是不是这样子:
    ...
      private
        { Private declarations }
        procedure ButtonOnClick(Sender: TObject);
    ...
    procedure TForm1.Button1Click(Sender: TObject);
    var
      b: TButton;
    begin
      b := TButton.Create(Self);
      with b do
      begin
        Caption := 'season';
        Parent := Self;
        b.Onclick := ButtonOnClick;
      end;
    end;procedure TForm1.ButtonOnClick(Sender: TObject);
    begin
      ShowMessage('Yeah!');
    end;
      

  3.   

    var
      button: TButton;
    begin
      button := TButton.Create(self);
      button.OnClick := MyButtonClick;//MyButtonClick是提前定义好的当前窗体的成员函数。
    end;
      

  4.   

    楼上在创建过程中最好把button的parent设置上
    就是这样
    var
      button: TButton;
    begin
      button := TButton.Create(self);
      button.parent := Form1;
      button.OnClick := New_OnClick;//自己预先定义的过程
      //这里只是对新创建的button赋予按下的事件 
      //并不是楼主说的“捕捉OnClick事件”
      //如果要捕捉,就应该拦截它的消息,可以通过Hook来完成了
    end;
      

  5.   

    这个我知道,能够Show ‘ok’也没有问题,关键我可能动态创建多个Button,每个Button的onclick都有不同的显示内容,因此我在创建的时候赋予不同Button不同的tag,这样我可以在noclick时取出Tag,然后再根据tag查询数据库,关键如何取出不同Button的tag?
      

  6.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
        Procedure aButtonOnClick(Sender:Tobject);
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}
    Procedure TForm1.aButtonOnClick(Sender:Tobject);
    begin
      showmessage(Tbutton(Sender).Caption);
    end;
    procedure TForm1.FormCreate(Sender: TObject);
    begin
      with Tbutton.Create(Self) do
      begin
        Parent:=Self;
        Name:='TestButton';
        caption:='jinjazz';
        top:=100;
        left:=100;
        Onclick:=aButtonOnClick;
      end;
    end;end.
      

  7.   

    have a tryprocedure TMainForm.Button1Click(Sender: TObject);
    var
      btn1,btn2 :TButton;
    begin
      btn1 := TButton.Create(Self);
      btn1.Parent := MainForm;
      btn1.Left := 10;
      btn1.Top := 10;
      btn1.Width := 80;
      btn1.Height := 23;
      btn1.Caption := 'create';
      btn1.Tag := 1;
      btn1.OnClick := MyClick;  btn2 := TButton.Create(Self);
      btn2.Parent := MainForm;
      btn2.Left := 100;
      btn2.Top := 10;
      btn2.Width := 80;
      btn2.Height := 23;
      btn2.Caption := 'destroy';
      btn2.Tag := 2;
      btn2.OnClick := MyClick;
    end;procedure TMainForm.MyClick(Sender :TObject);
    begin
      if (TButton(Sender)).Tag = 1 then
        showmessage('create')
      else
        showmessage('destroy');
    end;
      

  8.   

    谢谢,我后来自己摸索出来了unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, Buttons, ExtCtrls,typinfo;type
      TForm1 = class(TForm)
        Button1: TButton;
        Button2: TButton;
        procedure Button2Click(Sender: TObject);
        Procedure OnClickok(sender:Tobject);
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;  TBtnDraw = Class(TImage)
      Public
        ID: string;
      End;var
      Form1: TForm1;
      Btop1: integer;
      BtnName: string;implementationuses Unit2;{$R *.dfm}procedure TForm1.Button2Click(Sender: TObject);
      Var
      Img: TBtnDraw;
      imgArraw: TImage;
    begin
      Form2 := TForm2.Create(nil);
      With Form2 do
      begin
        ShowModal;
        Free;
      End;
      Img := TBtnDraw.Create(self);
      Img.Parent := Self;
      img.Left := 50;
      img.Top := bTop1;
      img.Width := 80;
      img.Height := 24;
      img.Picture.LoadFromFile('F:\bt.bmp');
      img.ID := BtnName;
      img.OnClick := Onclickok;
      Img.Cursor := CrHandPoint;  ImgArraw := Timage.Create(self);
      ImgArraw.Parent := Self;
      ImgArraw.Left := 65;
      ImgArraw.Top := btop1+23;
      ImgArraw.Width := 50;
      ImgArraw.Height := 50;
      ImgArraw.Transparent := true;
      ImgArraw.Picture.LoadFromFile('F:\Arrow.bmp');
      Btop1 := Btop1+60;
    end;procedure TForm1.OnClickok(sender:Tobject);
    begin
      ShowMessage(TBtnDraw(sender).ID);
    end;procedure TForm1.FormCreate(Sender: TObject);
    begin
      Btop1 := 50;
    end;end.