我正在尝试使用操作来控制控件的可见性.我的代码如下所示:
Pascal文件
unit Unit1; interface uses Windows,Messages,SysUtils,Variants,Classes,Graphics,Controls,Forms,Dialogs,ActnList,StdCtrls; type TForm1 = class(TForm) Button1: TButton; ActionList1: TActionList; Action1: TAction; CheckBox1: TCheckBox; procedure Action1Update(Sender: TObject); end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Action1Update(Sender: TObject); begin (Sender as TAction).Visible := CheckBox1.Checked; end; end.
表格文件
object Form1: TForm1 object Button1: TButton Left = 8 Top = 31 Action = Action1 end object CheckBox1: TCheckBox Left = 8 Top = 8 Caption = 'CheckBox1' Checked = True State = cbChecked end object ActionList1: TActionList Left = 128 Top = 8 object Action1: TAction Caption = 'Action1' OnUpdate = Action1Update end end end
首次运行表单时,按钮可见,并选中复选框.然后我取消选中复选框,按钮消失.当我重新选中复选框时,该按钮无法重新出现.
我认为这个的原因可以在TCustomForm.UpdateActions中的以下本地函数中找到:
procedure TraverseClients(Container: TWinControl); var I: Integer; Control: TControl; begin if Container.Showing and not (csDesigning in Container.ComponentState) then for I := 0 to Container.ControlCount - 1 do begin Control := Container.Controls[I]; if (csActionClient in Control.ControlStyle) and Control.Visible then Control.InitiateAction; if (Control is TWinControl) and (TWinControl(Control).ControlCount > 0) then TraverseClients(TWinControl(Control)); end; end;
Control.Visible的检查似乎阻止我的行动再次有机会自我更新.
我是否正确诊断了该问题?这是设计还是我应该提交质量控制报告?有没有人知道一个解决方法?