gpt4 book ai didi

delphi - 如何使用操作来确定控件的可见性?

转载 作者:行者123 更新时间:2023-12-03 14:50:58 25 4
gpt4 key购买 nike

我正在尝试使用操作来控制控件的可见性。我的代码如下所示:

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 event 应该为你做。当复选框被选中时,调用 Action1.Update,或者如果这不起作用,则调用 Action1.Visible := True

您还可以将操作更新代码放入 TActionList.OnUpdate 事件中。

关于delphi - 如何使用操作来确定控件的可见性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10127685/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com