문제

I'm facing a problem that I can't find a way in Delphi to solve it.

I have a pop up menu and I have an event for when the person select this option, what I want to do is broadcast this event for any object that registry to it.

The problem that I'm facing is that I can just registry to the event if I have an instance of the pop up menu created, but if I do create an instance just to listen do this event still it is a different instance and I wont be able to listen to it.

How can I archive this goal of have many objects listen to an event of one object in Delphi?

Thanks

도움이 되었습니까?

해결책

Delphi does not natively support multicasting of events. You have to create your own mechanism for that. The simplest solution is to store your registered objects/handlers in a list, then loop through that list when the event is triggered, calling each object/handler as needed. For example:

Simulating multicast events in Win32 Delphi

Allen Bauer wrote a series of blog articles about a more advanced way to implement multicasting events in Delphi:

Multicast events using generics
Multicast Events - the cleanup
Multicast Events - the finale

Getting Allen Bauer's TMulticastEvent<T> working

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top