如何在每两个不同的类上一起触发事件
我在一个单元上有两个对象类,是否可以触发这两个事件?假设 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在创建两个类时为其分配相同的事件处理程序(伪代码):
这样,当从任一类触发事件时,它将最终出现在 MyHandler() 代码中。
You can assign the same Event handler to both classes when they are created (Pseudo-code):
This way when the event fires from either class, it'll end up in the MyHandler() code.