delphi - 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);
  if ValueExists(Section, Comp.Name) then
     if Comp.InheritsFrom(TAction)
     then TAction(Comp).Checked:= ReadBool(Section, Comp.Name, FALSE)              
        if Comp.InheritsFrom(TCheckBox) etc 

I use my class like this:

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


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

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;
 MyIniFile:= TMyIniFile.Create('Main Form');;     // <------ This reads the 'Checked' property of MyAction. It should execute the actMyActionExecute but it doesn't. 
 assert(MyAction.Checked);     //  <---- Yes, it is checked 

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

Question: Why actMyActionExecute is not called when is executed?

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

1 Answer

  1. Leander- Reply


    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.

Leave a Reply

Your email address will not be published. Required fields are marked *

You can use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>