如何在每两个不同的类上一起触发事件

发布于 2024-09-03 12:12:31 字数 1326 浏览 7 评论 0原文

我在一个单元上有两个对象类,是否可以触发这两个事件?假设 FIRSTCLASS 事件被触发,SECONDCLASS 也会被触发吗?

假设……

//{Class 1}-------------------------------------------------------------
type
   TOnEventTrigger = procedure(Sender : Tobject; Value :integer); 
   TMyFirstClass = Class(Tcomponent)
   private
      ....
   public
      ....
      propert OnEventTrigger : TOnEventTrigger read Fevent write Fevent;
   end;

procedure TMyFirstClass.FEvnt(Sender : Tobject; Value :integer); 
begin
    // here is normaly triggers the event //
    if Assigned(OnEventTrigger) then
       OnEventTrigger(Self,FSomevalue);

    // POSTMessage(GetForegroundWindow,WM_USER+3,0,0);
    // this is what i did here to get the result of FSomevalue
    // but this is not ideal. It work only on focus window.

end;

//{Class 2}-------------------------------------------------------------

type
   TOnEventTrigger = procedure(Sender : Tobject; Value :integer); 
   TMySecondClass = Class(Tobject)
   private
      ....
   public
      ....
      property OnEventTrigger : TOnEventTrigger; read Fevent write Fevent;
   end;

procedure TMySecondClass.FEvnt(Sender : Tobject; Value :integer); 
begin
    // I wanted here to trigger, whenenver the above is fired //
    if Assigned(OnEventTrigger) then
       OnEventTrigger(Self,FSomevalue);
end;

I have two object class on a single unit, is it posible to trigger the two events? let say the FIRSTCLASS event is fired, The SECONDCLASS also will fired?

Assuming......

//{Class 1}-------------------------------------------------------------
type
   TOnEventTrigger = procedure(Sender : Tobject; Value :integer); 
   TMyFirstClass = Class(Tcomponent)
   private
      ....
   public
      ....
      propert OnEventTrigger : TOnEventTrigger read Fevent write Fevent;
   end;

procedure TMyFirstClass.FEvnt(Sender : Tobject; Value :integer); 
begin
    // here is normaly triggers the event //
    if Assigned(OnEventTrigger) then
       OnEventTrigger(Self,FSomevalue);

    // POSTMessage(GetForegroundWindow,WM_USER+3,0,0);
    // this is what i did here to get the result of FSomevalue
    // but this is not ideal. It work only on focus window.

end;

//{Class 2}-------------------------------------------------------------

type
   TOnEventTrigger = procedure(Sender : Tobject; Value :integer); 
   TMySecondClass = Class(Tobject)
   private
      ....
   public
      ....
      property OnEventTrigger : TOnEventTrigger; read Fevent write Fevent;
   end;

procedure TMySecondClass.FEvnt(Sender : Tobject; Value :integer); 
begin
    // I wanted here to trigger, whenenver the above is fired //
    if Assigned(OnEventTrigger) then
       OnEventTrigger(Self,FSomevalue);
end;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

裂开嘴轻声笑有多痛 2024-09-10 12:12:31

您可以在创建两个类时为其分配相同的事件处理程序(伪代码):

Form1 = class()
private
  { ... }
protected
  A : TMyFirstClass;
  B : TMySecondClass;
  procedure MyHandler(Sender: TObject; Value: Integer);
end;

procedure Form1.CreateForm();
Begin
     { Create A & B }
     A := TMyFirstClass.Create(Self);
     B := TMySecondClass.Create(Self);

     { Assign Event Handler to both classes }
     A.OnEventTrigger := MyHandler;
     B.OnEventTrigger := MyHandler;
End;

procedure Form1.MyHandler(Sender: TObject; Value: Integer);
Begin
     ShowMessage('Event from '+Sender.ClassName+'. Value='+IntToStr(Value));
End;

这样,当从任一类触发事件时,它将最终出现在 MyHandler() 代码中。

You can assign the same Event handler to both classes when they are created (Pseudo-code):

Form1 = class()
private
  { ... }
protected
  A : TMyFirstClass;
  B : TMySecondClass;
  procedure MyHandler(Sender: TObject; Value: Integer);
end;

procedure Form1.CreateForm();
Begin
     { Create A & B }
     A := TMyFirstClass.Create(Self);
     B := TMySecondClass.Create(Self);

     { Assign Event Handler to both classes }
     A.OnEventTrigger := MyHandler;
     B.OnEventTrigger := MyHandler;
End;

procedure Form1.MyHandler(Sender: TObject; Value: Integer);
Begin
     ShowMessage('Event from '+Sender.ClassName+'. Value='+IntToStr(Value));
End;

This way when the event fires from either class, it'll end up in the MyHandler() code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文