Why OnUpdate for invisible components is not activated

When I make a component invisible by setting the connected TAction to invisible, the onupdate event no longer fires. To recreate, follow these steps:

  • Create a New VCL Forms Application
  • Drop the button, check box, and list of actions on the form.
  • Create a new action and connect a button to it.
  • Enter the following code for the OnExecute and OnUpdate actions:

    procedure TForm1.Action1Execute(Sender: TObject); begin ShowMessage('Test'); end; procedure TForm1.Action1Update(Sender: TObject); begin TAction(Sender).Enabled := not CheckBox1.Checked; TAction(Sender).Visible := TAction(Sender).Enabled; end; 

Launch the app. The button is visible and working correctly. Check the box and the button will disappear. Uncheck the box. The button is not displayed. In fact, if you set a breakpoint in Action1Update, you will never get to it. Why is this and how to fix it?

+2
source share
4 answers

No need to fix this, it works as designed. Only visible controls should update their state, so only actions whose associated controls are visible are updated. When you hide the button, there is no more reason to update the action.

+2
source

Ask OnUpdate to call only a separate procedure that does what is required. Then you can call this routine from other places. For this, action lists have been developed.

+1
source

I understand what you are trying to do, and it makes sense that you want it to work that way. However, here is a workaround for how it works.

You can also update other controls in OnUpdate . You are not limited to updating a control that receives a notification. Thus, in action for a control that defines visibility, you can set the visibility of other controls there. In your case, this checkbox:

Create a new action (Action2) and assign it to the checkbox.

Then in the checkbox action OnUpdate:

 procedure TForm1.Action2Update(Sender: TObject); begin Button1.Visible := TAction(Sender).Checked; end; 

Be sure to check the OnExecute box. Something simple, how wonderful it is:

 procedure TForm1.Action2Execute(Sender: TObject); begin TAction(Sender).Checked := not TAction(Sender).Checked; end; 

For me, this still makes logical sense. You can look in one place to see all the controls whose visibility depends on the checkbox selected.

+1
source

You can override the InitiateAction method on the form. This will happen whenever the application goes into standby, just like the OnUpdate event for each action.

+1
source

All Articles