delphi – 如何使用操作来确定控件的可见性?
发布时间:2020-12-15 04:15:52 所属栏目:大数据 来源:网络整理
导读:我正在尝试使用操作来控制控件的可见性.我的代码如下所示: Pascal文件 unit Unit1;interfaceuses Windows,Messages,SysUtils,Variants,Classes,Graphics,Controls,Forms,Dialogs,ActnList,StdCtrls;type TForm1 = class(TForm) Button1: TButton; ActionLis
我正在尝试使用操作来控制控件的可见性.我的代码如下所示:
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的检查似乎阻止我的行动再次有机会自我更新. 我是否正确诊断了该问题?这是设计还是我应该提交质量控制报告?有没有人知道一个解决方法? 解决方法
你的诊断是正确的.自从它们首次引入Delphi以来,动作就是这样运作的.
我希望它是设计的(可能是为了避免过度更新文本和隐形控件的其他视觉方面的优化),但这并不意味着设计是好的. 我能想象的任何解决方法都会涉及你的复选框代码直接操纵受影响的操作,这不是很优雅,因为它不应该知道它可能影响的其他一切 – 这就是OnUpdate事件应该为你做的事情.选中该复选框后,调用Action1.Update,如果不起作用,则Action1.Visible:= True. 您也可以将操作更新代码放在TActionList.OnUpdate事件中. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |