Delphi 如何简明地检查几个编辑框中的任何一个是否已更改其原始值?
我创建了一个包含3个TEdit、1个TButton和3个字符串和布尔变量的简单项目。如何创建过程或函数以设置按钮。启用:=当TEdit中的每个值都发生更改时为True。需要通过创建过程或函数来减少编码,而不是使用下面的代码Delphi 如何简明地检查几个编辑框中的任何一个是否已更改其原始值?,delphi,editbox,Delphi,Editbox,我创建了一个包含3个TEdit、1个TButton和3个字符串和布尔变量的简单项目。如何创建过程或函数以设置按钮。启用:=当TEdit中的每个值都发生更改时为True。需要通过创建过程或函数来减少编码,而不是使用下面的代码 type TForm1 = class(TForm) Edit1: TEdit; Edit2: TEdit; Edit3: TEdit; btnSave: TButton; procedure FormCreate(Sender: TObject);
type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
btnSave: TButton;
procedure FormCreate(Sender: TObject);
procedure Edit1Exit(Sender: TObject);
private
{ Private declarations }
public
strWelcome, strTo, strThailand: String;
modify1, modify2, modify3 : Boolean;
{ Public declarations }
end;
一旦创建表单,我将3个字符串的值委托给3个TEdit.Text,并将修改变量设置为False
procedure TForm1.FormCreate(Sender: TObject);
begin
strWelcome := 'Welcome';
strTo := 'To';
strThailand:= 'Thailand';
modify1 := false;
modify2 := false;
modify3 := false;
Edit1.text := strWelcome;
Edit2.text := strTo;
Edit3.text := strThailand;
end;
3个TEdit的Onexit分配给Edit1Exit(发送方:TObject);检查文本值是否仍然等于初始值?如果一些TEdit.Text已更改,则将启用btnSave
procedure TForm1.Edit1Exit(Sender: TObject);
begin
if Edit1.Text = strWelcome then
modify1 := False
else
modify1 := True;
if Edit2.Text = strTo then
modify2 := False
else
modify2 := True;
if Edit3.Text = strThailand then
modify3 := False
else
modify3 := True;
btnSave.Enabled := modify1 or modify2 or modify3;
end;
创建过程或函数以减少上述代码的任何想法。:) 您可以使用数组使其更加简洁,但仅使用三项可能不值得。如果这真的是所有考虑中的代码,我会这样写:
procedure TForm1.Edit1Exit(Sender: TObject);
begin
btnSave.Enabled :=
(Edit1.Text <> strWelcome) or
(Edit2.Text <> strTo) or
(Edit3.Text <> strThailand);
end;
程序TForm1.Edit1Exit(发送方:TObject);
开始
btnSave.Enabled:=
(Edit1.Text strWelcome)或
(Edit2.Text strTo)或
(Edit3.Text-StrTailand);
结束;
您应该删除这三个布尔字段。它们不再被使用,在任何情况下都应该是局部变量,或者作为getter函数的属性公开。您应该将strXXX
变量转换为常量,因为我假设它们不会改变
我还建议给你的编辑控件命名 非常感谢@DavidHeffernan的回答。我有超过10个TEdit,如edCustname、edCustAddr、edCustAttn、edCustPostCode等,初始值将来自“select*from customer where customer.code=”c001“。我给所有的TEdit赋值。如何使用此案例的数组。将控件放入
TEdit
的数组中。将默认值加载到字符串的数组中。初始化for循环中的编辑控件。同样,在测试它们是否已被修改。仅供参考时,TEdit
有一个modified
属性,如果用户已编辑文本,则该属性为true,即使文本已恢复到原始值。我建议使用TAction
而不是TEdit.OnExit
事件,使用TAction.OnUpdate
事件根据TEdit
控件的当前状态启用/禁用按钮。@DavidHeffernan我的编辑控件设置为design to form。我不知道如何将它们放入TEdit数组中。请查找我的代码项目分配的链接,如下所示:Arr[0]:=Edit1;Arr[1]:=Edit2代码>等。