现要打开一个模态对话框,以让用户输入一个名称。
在对话框中,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事件中检查后,无论合法如否,窗口都关闭了.
没有达到预想的效果.
请问,应该在什么地方进行检查?怎么做?谢谢!
随时揭帖.

解决方案 »

  1.   

    ShowModal=mrOK 这句话就会把窗体关掉的!
      

  2.   

    procedure TdlgUserName.Button1Click(Sender: TObject);
    var
      theName:String;
    begin
      theName:=Edit1.Text;
      //检查是否为空
      if theName='' then
      begin
        Application.MessageBox('用户名不能为空','Alert');
        Edit1.SetFocus;
        Exit;  <<----------------------------------------
      end
      else
      begin
        //Utils.CheckDBUserName用于检查数据库是否已存在该用户名
        if Utils.CheckDBUserName(theName) then //不存在,可以继续
        begin
          //记入一个property中,然后
          ...
          ModalResult:=mrOK;
        end
        else
        begin
          Application.MessageBox('用户名不能为空','Alert');
          Edit1.SetFocus;
          Exit;  <<------------------------------------------
        end;
      end;
    end;在主Form中调用它如下:
    ....
    dlg.Create(Self);
    if dlg.ShowModal=mrOK then
      

  3.   

    begin
          Application.MessageBox('用户名不能为空','Alert');
          Edit1.SetFocus;
          Exit;//加入这一句,或者不要把button1修改为模态按钮/
    end;
      

  4.   

    我以前试过Shiyl((云淡)||(风清)) 的方法,两处都加了Exit,窗口还是关闭了。duduwolf(嘟嘟狼),如果不把button1设为模态,我在Edit1按回车后,不能激发关闭事件,
    界面不友好啊,
    没有好的方法吗?
      

  5.   

    现要打开一个模态对话框,以让用户输入一个名称。
    在对话框中,Button1是默认按钮.有一个文本框,需要在关闭之前检查文本框是否为空.
    我现在想到的是在Button1的Click事件中检查,代码如下:
    procedure TdlgUserName.Button1Click(Sender: TObject);
    begin
      if trim(edit1.text)='' 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;
      

  6.   

    if theName='' then
      begin
        Application.MessageBox('用户名不能为空','Alert');
        Edit1.SetFocus;
        abort;//加上这个就行了
      end
      

  7.   

    加入abort;还是关掉了,郁闷阿。