我想创建一个启用或禁用按钮的程序, 我可以用一个程序吗?例如:
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
我能这样做吗?
我找不到以简单的方式做到这一点的方法
答案 0 :(得分:6)
procedure MainForm.buttonsEnabled(AEnabled: Boolean);
begin
button1.Enabled := AEnabled;
button2.Enabled := AEnabled;
button3.Enabled := AEnabled;
end;
buttonsEnabled(True);
//buttonsEnabled(False);
答案 1 :(得分:5)
创建表单的属性:
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;
答案 2 :(得分:1)
多用途
第一个选项:
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;
第二种选择:
重新启用(或不启用)控件上的按钮:
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;