如何检查两个事件是否在Delphi中指向相同的过程? [英] How to check if two events are pointing to the same procedure in Delphi?

查看:245
本文介绍了如何检查两个事件是否在Delphi中指向相同的过程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个Button1.OnClick事件链接到Button1Click过程。我也有Button2.OnClick链接到一些其他的过程。我如何检查两个事件是否与运行时链接到不同或相同的过程?



我试图测试:




  • Button1.OnClick = Button2.OnClick,但这给了我一个错误(实际参数不足)

  • @(Button1.OnClick)= @ Button2.OnClick),再次出错(实际参数不足)



如何正确测试?

解决方案

方法引用可以分为两部分,指向对象的指针和方法本身的指针。在 System 中定义的名为 TMethod 的便捷记录类型,可以让我们进行分解。 p>

有了这些知识,我们可以这样写:

  function SameMethod (AMethod1,AMethod2:TNotifyEvent):boolean; 
begin
result:=(TMethod(AMethod1).Code = TMethod(AMethod2).Code)
和(TMethod(AMethod1).Data = TMethod(AMethod2).Data);
结束

希望这有帮助。 :)



修改:只是为了更好地布局我在这里解决的问题(在评论中提到)



如果您有两种表单,都从同一基类实例化:

  Form1:= TMyForm.Create(nil); 
Form2:= TMyForm.Create(nil);

,并将这些表单中的相同方法分配给两个按钮:

  Button1.OnClick:= Form1.ButtonClick; 
Button2.OnClick:= Form2.ButtonClick;

比较两个 OnClick 属性,你会发现 Code 是一样的,但是 Data 是不同的。这是因为它是相同的方法,但是在类的两个不同的实例中...



现在,如果在同一个对象上有两个方法:

  Form1:= TMyForm.Create(nil); 

Button1.OnClick:= Form1.ButtonClick1;
Button2.OnClick:= Form1.ButtonClick2;

然后他们的数据将是一样的,但他们的代码将不同。


Say I have a Button1.OnClick event linked to Button1Click procedure. I also have Button2.OnClick linked to some other procedure. How do I check that both events are linked to different or same procedure from runtime?

I tried to test if:

  • Button1.OnClick = Button2.OnClick, but that gave me an error (Not enough actual parameters)
  • @(Button1.OnClick) = @(Button2.OnClick), error again (Not enough actual parameters)

How do I test it properly?

解决方案

A method reference can be broken down in to two parts, the pointer to the object and the pointer to the method itself. There is a convenient record type defined in the System unit called TMethod that allows us to do that break down.

With that knowledge, we can write something like this:

function SameMethod(AMethod1, AMethod2: TNotifyEvent): boolean;
begin
  result := (TMethod(AMethod1).Code = TMethod(AMethod2).Code) 
            and (TMethod(AMethod1).Data = TMethod(AMethod2).Data);   
end;

Hope this helps. :)

Edit: Just to lay out in a better format the problem I am trying to solve here (as alluded to in the comments).

If you have two forms, both instantiated from the same base class:

Form1 := TMyForm.Create(nil);
Form2 := TMyForm.Create(nil);

and you assign the same method from those forms to the two buttons:

Button1.OnClick := Form1.ButtonClick;
Button2.OnClick := Form2.ButtonClick;

And compare the two OnClick properties, you will find that the Code is the same, but the Data is different. That is because it's the same method, but on two different instantiations of the class...

Now, if you had two methods on the same object:

Form1 := TMyForm.Create(nil);

Button1.OnClick := Form1.ButtonClick1;
Button2.OnClick := Form1.ButtonClick2;

Then their Data will be the same, but their Code will be different.

这篇关于如何检查两个事件是否在Delphi中指向相同的过程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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