Mega Code Archive

 
Categories / Delphi / Examples
 

Broadcast message within your application

Title: Broadcast message within your application Question: An elegant way to inform all controls within an application. Direct references to all the TEdits or TCombos can be avoided ! Answer: The Delphi class TWinControl has a handy function 'broadcast' to inform all controls within a form. An object needs an event-handler to react on these messages. The message queue can be interrupted if Message.Result returns a value 0. var i: integer; hMessage: TMessage; begin hMessage.Msg := WM_USER + 1; hMessage.WParam := 0; hMessage.LParam := 0; for i := 0 to Screen.FormCount-1 do Screen.Forms[i].Broadcast(hMessage); end; With the help of TScreens its possible to send the message to all forms of the application. This is a event-handler: TMyButton = class(TButton) protected procedure EventHandler(var Message: TMessage); message WM_USER + 1; end; ... procedure TMyButton.EventHandler(var Message: TMessage); begin // commands Message.Result := 0; // Event continues end; Note that this communication is not asynchron !