How can I see who triggered an action in Delphi?

前端 未结 7 1340
别那么骄傲
别那么骄傲 2021-02-05 12:42

When a TAction event fires, the \"Sender\" is always the action itself. Usually that\'s the most useful, but is it somehow possible to find out who triggered the action\'s OnExe

7条回答
  •  我在风中等你
    2021-02-05 13:05

    Ok, in the meanwhile I think I found a workable solution..

    I can have all controls use the same action; I just need to override their OnClick event handler, and I just need a single handler for all of them.

    I'm still interested to know if it's possible to find out which control triggered the action, but for my current application I'm using a solution that's similar to the code below:

    unit Example;
    
    interface
    
    uses
      Windows, Classes, Forms, Dialogs, Controls, ActnList, StdCtrls;
    
    type
      TForm1 = class(TForm)
        Button1: TButton;
        Button2: TButton;
        actDoStuff: TAction;
        procedure actDoStuffExecute(Sender: TObject);
        procedure ButtonClick(Sender: TObject);
        procedure FormCreate(Sender: TObject);
      end;
    
    var
      Form1: TForm1;
    
    implementation
    
    {$R *.dfm}
    
    procedure TForm1.actDoStuffExecute(Sender: TObject);
    begin
      ShowMessage('Button '+TControl(Sender).Name +' was clicked')
    end;
    
    procedure TForm1.ButtonClick(Sender: TObject);
    begin
      actDoStuffExecute(Sender)
    end;
    
    procedure TForm1.FormCreate(Sender: TObject);
    begin
      Button1.OnClick := ButtonClick;
      Button2.OnClick := ButtonClick
    end;
    
    end.
    

提交回复
热议问题