简单问题:如何在对话框关闭之前进行有效性检查。立即揭帖
现要打开一个模态对话框,以让用户输入一个名称。
在对话框中,Button1是默认按钮.有一个文本框,需要在关闭之前检查文本框是否为空.
我现在想到的是在Button1的Click事件中检查,代码如下:
procedure TdlgUserName.Button1Click(Sender: TObject);
var
theName:String;
begin
theName:=Edit1.Text;
//检查是否为空
if theName='' then
begin
Application.MessageBox('用户名不能为空','Alert');
Edit1.SetFocus;
end
else
begin
//Utils.CheckDBUserName用于检查数据库是否已存在该用户名
if Utils.CheckDBUserName(theName) then //不存在,可以继续
begin
//记入一个property中,然后
...
ModalResult:=mrOK;
end
else
begin
Application.MessageBox('用户名不能为空','Alert');
Edit1.SetFocus;
end;
end;
end;
在主Form中调用它如下:
....
dlg.Create(Self);
if dlg.ShowModal=mrOK then
//...插入用户名到数据库
....
但是运行结果显示,在Button1的Click事件中检查后,无论合法如否,窗口都关闭了.
没有达到预想的效果.
请问,应该在什么地方进行检查?怎么做?谢谢!
随时揭帖.