RAD Studio |  |

Broadcasting a Message to All Controls in a Form
See Also
Collapse All

When your component changes global settings that affect all of the controls in a form or other container, you may want to send a message to those controls so that they can update themselves appropriately. Not every control may need to respond to the notification, but by broadcasting the message, you can inform all controls that know how to respond and allow the other controls to ignore the message.

To broadcast a message to all the controls in another control, use the Broadcast method. Before you broadcast a message, you fill out a message record with the information you want to convey.

Copy Code

    var
    Msg: TMessage;
    begin
      Msg.Msg := MY_MYCUSTOMMESSAGE;
      Msg.WParam := 0;
      Msg.LParam := Longint(Self);
      Msg.Result := 0;

Copy Code

    TMessage Msg;
    Msg.Msg = MY_MYCUSTOMMESSAGE;
    Msg.WParam = 0;
    Msg.LParam = (int)(this);
    Msg.Result = 0;

Then, pass this message record to the parent of all the controls you want to notify. This can be any control in the application. For example, it can be the parent of the control you are writing:

Copy Code

    Parent.Broadcast(Msg);

Copy Code

    Parent->Broadcast(Msg);

It can be the form that contains your control:

Copy Code

    GetParentForm(self).Broadcast(Msg);

Copy Code

    GetParentForm(this)->Broadcast(Msg);

It can be the active form:

Copy Code

    Screen.ActiveForm.Broadcast(Msg);

Copy Code

    Screen->ActiveForm->Broadcast(Msg);

It can even be all the forms in your application:

Copy Code

    for I:= 0 to Screen.FormCount - 1 do
      Screen.Forms[I].Broadcast(Msg);

Copy Code

    for (int i = 0; i < Screen->FormCount; i++)
      Screen->Forms[i]->Broadcast(Msg);

See Also

Sending a Message That Does Not Execute Immediately

Sending a Message Using the Windows Message Queue

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!