delphi 启用或禁用按钮的布尔程序

delphi boolean procedure for enable or disable button

我想创建一个程序来启用或禁用按钮, 我可以通过一个程序完成吗?例如像这样:

Procedure MainForm.buttonsEnabled(boolean);
BEGIN
if result=true then
begin
button1.enabled:=True;
button2.enabled:=True;
button3.enabled:=True;
end else
begin
button1.enabled:=false;
button2.enabled:=false;
button3.enabled:=false;
end;
END;

当我调用程序来禁用或启用按钮时,我可以像这样调用它

buttonsEnabled:=True;// to enable button
buttonsEnabled:=False;// to disable button

我可以那样做吗? 我找不到简单的方法来做到这一点

procedure MainForm.buttonsEnabled(AEnabled: Boolean);
begin
  button1.Enabled := AEnabled;
  button2.Enabled := AEnabled;
  button3.Enabled := AEnabled;
end;

buttonsEnabled(True);
//buttonsEnabled(False);

创建一个 属性 的形式:

type
  TMyForm = class(TForm)
  private
    procedure SetButtonsEnabled(Value: Boolean);
  public // or private perhaps, depending on your usage
    property ButtonsEnabled: Boolean write SetButtonsEnabled;
  end;

这样实现:

procedure TMyForm.SetButtonsEnabled(Value: Boolean);
begin
  button1.Enabled := Value;
  button2.Enabled := Value;
  button3.Enabled := Value;
end;

然后就可以随意使用了:

ButtonsEnabled := SomeBooleanValue;

多用途

第一个选项:

Procedure EnabledDisableControls(Ctrls:Array of TControl; Enabled:Boolean);
var
  C:TControl;
begin
  for C in Ctrls do
    C.Enabled:=Enabled;
end;


//calling example : 
procedure TForm1.BtnTestClick(Sender: TObject);
begin
  EnabledDisableControls([Button1, Button2, Button3], false {or True});
end;

第二个选项:
Recrusively(或不) enabling/disabling 控件上的按钮:

Procedure EnableDisableButtonsOnControl(C:TControl; Enabled:Boolean; Recrusive:Boolean);
var
  i:integer;
begin
  if C is TButton {or TBitButton or anything you need} then
    C.Enabled:=Enabled
  else if C is TWinControl then
    for i := 0 to TWinControl(C).ControlCount-1 do
    begin
      if TWinControl(C).Controls[i] is TButton then
        TButton(TWinControl(C).Controls[i]).Enabled:=Enabled
      else
      if Recrusive then
        EnableDisableButtonsOnControl(TWinControl(C).Controls[i],Enabled,true);
    end;
end;

//calling example :
procedure TForm1.BtnTestClick(Sender: TObject);
begin
  //disable all buttons on Form1:  
  EnableDisableButtonsOnControl(Self, false, false {or true});
  ...
  //disable all buttons on Panel1:  
  EnableDisableButtonsOnControl(Panel1, false, false {or true});
  ...
  //disable all buttons on Panel1 recursively:  
  EnableDisableButtonsOnControl(Panel1, false, true);
end;