2

I want to simplify a bit the saving of a form state to disk. I use my own INI file class derived from TIniFile to read the status of "all" controls present of form. Something like this:

procedure TMyIniFile.Read(Comp: TComponent);
begin
  if ValueExists(Section, Comp.Name) then
   begin
     if Comp.InheritsFrom(TAction)
     then TAction(Comp).Checked:= ReadBool(Section, Comp.Name, FALSE)              
     else
        if Comp.InheritsFrom(TCheckBox) etc 
  end;
end;

I use my class like this:

TYPE
 TformTester = class(TForm)
   MyAction: TAction;
   procedure actMyActionExecute(Sender: TObject);

...

procedure TformTester.FormDestroy(Sender: TObject);
VAR
   MyIniFile: TMyIniFile;
begin
 MyAction.Checked:= true;
 MyIniFile:= TMyIniFile.Create('Main Form');
 MyIniFile.write(MyAction);  // <------ This saves the 'Checked' property of MyAction.
 ...
end;

I verified the INI file and the state is correctly saved (true/false) depending on property's state at shut down.

procedure TformTester.FormStartUp;
VAR MyIniFile: TMyIniFile;
begin
 MyIniFile:= TMyIniFile.Create('Main Form');
 MyIniFile.read(MyAction);     // <------ This reads the 'Checked' property of MyAction. It should execute the actMyActionExecute but it doesn't. 
 assert(MyAction.Checked);     //  <---- Yes, it is checked 
 ...
end;


procedure TformTester.MyActionExecute(Sender: TObject);
begin
 if MyAction.Checked
 then Caption:= 'Action checked'
 else Caption:= 'Action is un-checked!';
end;

Question: Why actMyActionExecute is not called when MyIniFile.read(MyAction) is executed?

PS: MyIniFile.read(MyCheckbox) works if instead of TAction I pass anything else, for example a checkbox. I mean MyCheckbox.OnClick is executed!

Gabriel
  • 20,797
  • 27
  • 159
  • 293

1 Answers1

4

Action OnExecute is fired when linked controls are invoked. For instance a button is pressed or menu item selected. Or it is fired if you explicitly call Execute on the event.

The OnExecute event won't be fired when you modify any of its properties. That is by design and quite reasonable. This event fires when users action something. Not when the programmer sets up the action.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • The action is linked (at design time) to a checkbox. Sorry. I should have mentioned that. – Gabriel Sep 16 '17 at 12:43
  • 1
    Setting an action's properties won't invoke its events. That is by design. – David Heffernan Sep 16 '17 at 12:44
  • "Setting an action's properties won't invoke its events" - ok. So that is my problem! Thanks. – Gabriel Sep 16 '17 at 12:45
  • Any suggestions on how to achieve this (restoring the Checked property AND firing the event)? I just call OnExecute in my ini class? – Gabriel Sep 16 '17 at 12:48
  • Call the Execute method of the action when you want the event handler to run. The thing is though, I don't really see why you are saving the form state rather than the model state. Your program design seems back to front. – David Heffernan Sep 16 '17 at 12:50
  • "model state" - you are right, my lib is not complete. it only saves the most important properties like checkbox.checked, spinbox.value, etc. But it does it with minimum effort/code :) It worked quite nice until I added TAction. I was looking for a COMPLETE library that could save a whole form, with all properties of all control. None (no good/non-experimental one) was found. – Gabriel Sep 16 '17 at 12:53
  • That's the wrong approach. When you tweak your UI you can't load old data files. Your mistake us using UI controls as your data model. You need to separate the model from the presentation. – David Heffernan Sep 16 '17 at 12:54
  • I see your point and I TOTALLY agree with it. But it is only half my fault :) Delphi tough us that the first thing we do, we open a form and put a bunch of controls on it. THEN you attach objects to those controls. This is the reverse of what (you say) should happen. At this point many of my (small) programs are built around the DFM. I am sure MOST delphi programmers took the same path (leading later to dark/entangled forests). – Gabriel Sep 16 '17 at 12:59
  • One day, when there will be PLENTY of time (never) I will convert my old programs. Until then I am forced to continue into my oldest mistake (which means that my custom INI file class will be long-time still in use). – Gabriel Sep 16 '17 at 13:01