Jump to content
Sign in to follow this  
XylemFlow

Can I set a property within a TFrame for all instances of the frame at run time?

Recommended Posts

I have created a TFrame at design time using the form designer. I then create several instances of the frame at run time. Is there a way to set a property of the underlying TFrame for all instances at once instead of having to iterate over all instances? For example, when a user changes the language at run time the captions and hints of the buttons in the TFrame need to be updated for all instances.

Edited by XylemFlow

Share this post


Link to post

I don't think there is an easy solution. If you really wanna go that route, you would need to intercept the instance creation process by e.g. overwriting the NewInstance method of your FrameXY class. Within that you could then add each created instance into a class var array or a class var TList. You could then e.g add a class procedure "SetLanguage" to your class from within you could iterate over all your instances and delegate this call to a regular instance method.

Share this post


Link to post

Another approach would be to declare a TMessage descendant that the frame (and probably also any form) subscribes to during creation and unsubscribes from on destruction. Then you can broadcast this message when the language changes.

uses
  System.Messaging;
  
type
  TLanguageMessage = class(TMessage<string>);

...

procedure TMyFrame.HandleLanguageMessage(const Sender: TObject; const M: TMessage);
begin
  var msg := M as TLanguageMessage;
  SwitchToLanguage(M.Value);
end;

... TMyFrame.Create
  FLanguageMessageID := TMessageManager.DefaultManager.SubscribeToMessage(TLanguageMessage, HandleLanguageMessage);
  
... TMyFrame.Destroy
  TMessageManager.DefaultManager.Unsubscribe(TLanguageMessage, FLanguageMessageID, True);
  
... NotifyLanguage
  TMessageManager.DefaultManager.SendMessage(nil, TLanguageMessage.Create('DE'));

  
  
    

 

  • Like 2

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×