Sunday, April 19, 2009

Implementing MultiCast Events for Win32 Delphi's TDataset and TField Descendants

In Win32 Delphi programming, an event of a control is handled by only one event hanling procedure. For example, for a button control ("Button1") on a form ("Form1"), the OnClick event is handled by only one "procedure TForm1.ButtonClick(Sender: TObject) ;".

Win32 Delphi does not allow multicast events - only one procedure can be assigned as an event handler for a control event.

With Delphi for .NET a notion of multicast events was introduced. A developer can assign two or more methods to be called when an event gets fired.

Multicast events in Win32 Delphi
Since anything can be done, you can create a class in Win32 Delphi to simulate multicast events. The article "Simulating multicast events in Win32 Delphi" goes into details and provides a sceleton class to help you implement multicast events in Delphi for Win32.
When creating database applications in Win32 Delphi you might find the multicast idea useful - to have more than one procedure execute when the OnAfterScroll event fires for the TDataset descendant, for example.
The "TMultiDsEvent" class enables you to add multicast event handlers feature TDataSet and TField descendants.

MultiEvent = TMultiDsEvent.Create(Form1) ;

Delegate.AddEventHandler(Table,DSE_AFTEROPEN,afteropen2) ;
Delegate.AddEventHandler(TablePRP_FONERES,FLD_ONCHANGE,onchange2) ;
Delegate.AddEventHandler(TablePRP_FONERES,FLD_ONCHANGE,onchange3) ;


No comments: