views:

58

answers:

1

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;
+2  A: 

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.

K.Sandell