Broadcasting a Message to All Controls in a Form

From RAD Studio
Jump to: navigation, search

Go Up to Sending Messages


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.

Delphi:

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

C++:

 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:

Delphi:

 Parent.Broadcast(Msg);

C++:

 Parent->Broadcast(&Msg);

It can be the form that contains your control:

Delphi:

 GetParentForm(self).Broadcast(Msg);

C++:

 GetParentForm(this)->Broadcast(&Msg);

It can be the active form:

Delphi:

 Screen.ActiveForm.Broadcast(Msg);

C++:

 Screen->ActiveForm->Broadcast(&Msg);

It can even be all the forms in your application:

Delphi:

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

C++:

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

See Also