我正在尝试使用操作来控制控件的可见性。我的代码如下所示:
帕斯卡文件
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
似乎阻止了我的行动有机会再次更新自己。
我是否正确诊断了问题?这是设计使然还是我应该提交 QC 报告?有谁知道解决方法?