Why doesn't OnUpdate trigger for invisible components [duplicate]

吃可爱长大的小学妹 提交于 2019-12-06 11:28:06

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

Have the OnUpdate only call a separate routine that does what is required. Then you can call that routine from other places. Action lists were designed for that.

I understand what you're trying to do, and it makes sense that you would want it to work that way. However, here's a workaround for the way it does work.

You can update other controls in an OnUpdate also. You're not limited to updating the control that receives the notification. So, in the action for the control that determines visibility, you can set the visibility of the other controls there. In your case, that's the checkbox:

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

Then in the checkbox action's OnUpdate:

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

Be sure to assign an OnExecute to the checkbox as well. Something as simple as this is fine:

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

To me, this still makes logical sense. You'll be able to look in one spot to see all of the controls whose visibility relies on that checkbox being set.

You can override the InitiateAction method on the form. This will happen whenever the application goes idle, just as on OnUpdate event does for each action.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!