Recent

Author Topic: Message  (Read 1774 times)

Alexandr R

  • New Member
  • *
  • Posts: 33
Message
« on: July 11, 2025, 02:12:40 pm »
Good day.
There are two modules "Calc" and "Setting". When data changes in the "Setting" module are completed, I need to notify the "frmCalc" window about this event. What is the best way to do this? How can I send a message from one module to another (I don't know how to do this)? Or, given that the "frmSetting" window is modal, should I use the frmCalc.OnActivate event?
(OS: Windos 10 LTSC)

CM630

  • Hero Member
  • *****
  • Posts: 1689
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: Message
« Reply #1 on: July 11, 2025, 02:29:08 pm »
Do you need to close the Settings form when the button is presed (“OK” behaviour) or you need to do the changes without closing the Settings form (“Apply” behaviour)?
In the second case, maybe the caller form does not need to know that the button is pressed; you should call some update routines when “Apply” is pressed.
Лазар 4,4 32 bit (sometimes 64 bit); FPC3,2,2

cdbc

  • Hero Member
  • *****
  • Posts: 2787
    • http://www.cdbc.dk
Re: Message
« Reply #2 on: July 11, 2025, 02:55:24 pm »
Hi
Use the observer pattern, that's already built-in in the LCL...  8-)
In module 'calc' you just implement 'IFPObserver' in
Code: Pascal  [Select][+][-]
  1. TfrmCalc = class(TForm,IFPObserver)
and then the compiler wants you to implement a method -> do that.
When you show the settings form, you attach the 'frmcalc' as an observer:
Code: Pascal  [Select][+][-]
  1. frmSettings.FPOAttachObserver(frmCalc);
  2. frmSettings.Showmodal;
  3. frmSettings.FPODetachObserver(frmCalc);
When you click apply in 'Settings' you call 'FPONotifyObservers()'.
When the settings form is finished, you just detach 'frmCalc' from 'frmSettings', so you have no dangling references...
Easy Peasy  :D
Regards Benny
« Last Edit: July 11, 2025, 02:58:24 pm by cdbc »
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12345
  • Debugger - SynEdit - and more
    • wiki
Re: Message
« Reply #3 on: July 11, 2025, 03:07:40 pm »
Good day.
There are two modules "Calc" and "Setting". When data changes in the "Setting" module are completed, I need to notify the "frmCalc" window about this event. What is the best way to do this? How can I send a message from one module to another (I don't know how to do this)? Or, given that the "frmSetting" window is modal, should I use the frmCalc.OnActivate event?
(OS: Windos 10 LTSC)

frmSetting is modal => so you do have code that does: frmSetting.ShowModal?

Then I would recommend, to sent the notification after that. (Alternative in the OnFormClose or OnFormCloseQuery event....)
Code: Pascal  [Select][+][-]
  1.   frmSetting.ShowModal;
  2.   frmCalc.UpdateSettings; // call frmCalc

in frmCalc you add
Code: Pascal  [Select][+][-]
  1. type
  2.   TfrmCalc = TForm
  3.   public
  4.     procedure UpdateSettings;
  5.   end;
  6. ...
  7. procedure TfrmCalc.UpdateSettings;

And in frmSettings you must add the "unit" (you say module) in whih frmCalc is.
So in the
Code: Pascal  [Select][+][-]
  1. unit ModuleForFrmSetting;
  2. interface
  3. uses ..., ModuleForFrmCalc;

If you want to access frmSettings from UpdateSettings, then you can add in
Code: Pascal  [Select][+][-]
  1. unit ModuleForFrmCalc;
  2. interface
  3. ...
  4. implementation
  5. uses .... ,ModuleForFrmSettings;

Mind you, this is circular, and not the best way. But for now the quickest and easiest. (Otherwise you need a 3rd unit).



You can also make
Code: Pascal  [Select][+][-]
  1. procedure UpdateSettings(Setting1: TFoo; Setting2: TBar; ....);
and pass the settings.


Alexandr R

  • New Member
  • *
  • Posts: 33
Re: Message
« Reply #4 on: July 11, 2025, 03:31:31 pm »
 :D Thank you all very much, I will try. Most likely I will take advantage of the offer Martin

 

TinyPortal © 2005-2018