delphiinidelphi-xe7tinifile

TAction.OnExecute is not executed


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!


Solution

  • 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.