How can I see who triggered an action in Delphi?

前端 未结 7 1353
别那么骄傲
别那么骄傲 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:02

    There are situations where the same action should apply to similar controls. The problem with

    ShowMessage( (Sender as TAction).ActionComponent.Name );
    

    is that when the action is invoked by a say popup menu, you get the popup menu's name. You could use:

    procedure TMyForm.actMyActionExecute(Sender: TObject);
    var
      LMyControl: TMyControl;
    begin
      if Screen.ActiveControl.Name = 'MyControl1' then
        LMyControl = Sender as TMyControl
      else
        Exit;
      // Use the local variable for whatever needed
    end;
    

提交回复
热议问题