How can I use an action to determine a control's visibility?

前端 未结 3 1688
野趣味
野趣味 2021-01-05 07:33

I\'m trying to use an action to control the visibility of a control. My code looks like this:

Pascal file

unit Unit1;

interface

us         


        
3条回答
  •  悲哀的现实
    2021-01-05 07:44

    Yes your diagnosis is correct as Rob already said and explained. A work around is not to use individual TAction's OnUpdate event handlers, but to use the TActionList's OnUpdate event handler.

    procedure TForm1.ActionList1Update(Action: TBasicAction; var Handled: Boolean);
    begin
      Handled := True;
      Action1.Visible := CheckBox1.Checked;
    end;
    

    You do need at least one visible control with a linked action for this to work, otherwise the ActionList's OnUpdate handler won't be called either.

    Btw, when Actions and ActionLists were first introduced, Ray Konopka (Raize components) wrote a couple of articles on their implementation and gave very sound advice on how to use them. Ever since, I have adopted the practice of to use the OnExecute of each individual Action, but to use the OnUpdate of the ActionList. Also, the first thing I do in that handler is to set Handled to True so it won't be called more than necessary, and to only ever change an Action's Enabled property once in that handler so the GUI won't flicker as a result of turning it off and then on.

    Article by Ray Konopka is "Effectively using Action Lists" : http://edn.embarcadero.com/article/27058 Ray used to have three articles on his own site, but on embarcadero there is but one, but it may well be the "combined" version (don't have the originals handy).

提交回复
热议问题