-1

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

Icaro
  • 14,585
  • 6
  • 60
  • 75

1 Answers1

3

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

Community
  • 1
  • 1
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • That my sound damn, but I how can I do that from few different units. So for example I have the PopUp unit that is created by the main and sends the boadcast to the main form, then I have a object inside another inside another object, lets say rightPanel and inside comandGridPanel and inside Grid. If I want to make my event go all the way to the grid I need to have my rightPanel registry to the event, then it would create a new event to comandGridPanel and finally comandGridPanel would create a new event to grid? I think your solution is more horizontal and my problem is more vertical. – Icaro Feb 18 '14 at 21:28
  • Thanks Remy excellent response. I was researching for broadcast (the name we use in Object-C) not multicast so I wasn't getting much other then internet stuff. Thanks. – Icaro Feb 18 '14 at 21:34