我如何看到谁在Delphi中触发了一个动作? [英] How can I see who triggered an action in Delphi?

查看:134
本文介绍了我如何看到谁在Delphi中触发了一个动作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当动作甚至触发时,发件人始终是动作本身。通常这是最有用的,但是有可能找出谁触发了该动作的一次事件?



示例



假设您有一个表单,具有以下内容:




  • 2个按钮,称为 Button1 Button2

  • 1 TAction称为 actDoStuff



同样的操作分配给两个按钮。可以显示我点击了哪个按钮?



Example.dfm

 对象Form1:TForm1 
对象Button1:TButton
Action = actDoStuff
end
对象Button2:TButton
Action = actDoStuff
Left = 100
end
object actDoStuff:TAction
Caption ='Do Stuff'
OnExecute = actDoStuffExecute
end
end

Example.pas



<$ p $单位示例; p>
接口
使用Windows,类,表单,对话框,控件,ActnList,StdCtrls;

type
TForm1 = class(TForm)
Button1:TButton;
Button2:TButton;
actDoStuff:TAction;
procedure actDoStuffExecute(Sender:TObject);
结束

var
Form1:TForm1;

实现
{$ R * .dfm}

程序TForm1.actDoStuffExecute(发件人:TObject);
begin
ShowMessage('Button X被点击');
结束

结束。

目前唯一的解决方案是不使用按钮的动作属性,每个按钮的事件处理程序,并从那里调用actDoStuffExecute(),但是这种方式首先违反了使用操作的全部目的。



我不想对每个单独的控制也有一个专门的动作。上面的例子是我面临的问题的简化版本。我有一个带有可变数量菜单项(文件名)的菜单,每个菜单项基本上都要做同样的事情,除了加载另一个文件。对每个菜单项进行操作会有点愚蠢。

解决方案

尝试使用 ActionComponent 属性:

  ShowMessage((发件人为TAction).ActionComponent.Name); 

当我分别点击第一个和第二个按钮时,使用这个Button1和Button2


When an action even 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 onexecute event?

Example

Let's say you have a form with the following:

  • 2 buttons, called Button1 and Button2
  • 1 TAction called actDoStuff

The same action is assigned to both buttons. Is it possible to show which button I clicked?

Example.dfm

object Form1: TForm1
  object Button1: TButton
    Action = actDoStuff
  end
  object Button2: TButton
    Action = actDoStuff
    Left = 100
  end
  object actDoStuff: TAction
    Caption = 'Do Stuff'
    OnExecute = actDoStuffExecute
  end
end

Example.pas

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);
  end;

var
  Form1: TForm1;

implementation    
{$R *.dfm}

procedure TForm1.actDoStuffExecute(Sender: TObject);
begin
  ShowMessage('Button X was clicked');
end;

end.

The only solution I see at the moment is to not use the action property of buttons, but having an eventhandler for each button, and calling actDoStuffExecute() from there, but that sort of defies the whole purpose of using actions in the first place.

I don't want to have a dedicated action for each separate control either. The example above is a simplified version of the problem that I'm facing. I have a menu with a variable number of menu items (file names), and each menu item basically has to do the same thing, except for loading another file. Having actions for each menu item would be a bit silly.

解决方案

Try using the ActionComponent property:

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

Using this I get "Button1" and "Button2" when I click the first and second button respectively.

这篇关于我如何看到谁在Delphi中触发了一个动作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆