私は、コントロールの可視性を制御するアクションを使用しようとしています。フォームが最初のボタンが表示され、チェックボックスがチェックされている実行するとアクションを使用してコントロールの可視性を判断するにはどうすればよいですか?
パスカルファイル
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レポートを提出する必要がありますか?誰かが回避策を知っていますか?
なぜ、あなたは['OnUpdate'](http://docwiki.embarcadero.com/Libraries/en/Vcl.ActnList.TAction.OnUpdate)を使用していますか? – TLama
@tlama:これがそのためです。ほとんどの場合、 'Visible'ではなく' Enabled'を設定していると思います。 –
これはバグで、少なくともMisbegotten-Featureです。任意のアクションでEnabledとVisibleを設定できる必要があります。リンクされたコントロールは、Enabled値とVisible値を含むようにリフレッシュする必要があります。ここでActionUpdateが必要な場合は、実行しようとしている種類のデカップリングが解除されます。これでチェックボックスをアクションに結合する必要があります。私は、OnUpdateを使用して可視に変更するのをやめ、単にアクションを可視または不可視に設定するだけで、onclickのチェックボックスに1行で表示します。 –