为何SetFocus不起作用了呢,我的程序是MDI窗体,程序启动时有一个登陆窗体,用来作用户登陆验证,如果登陆验证出错,则用来输入用户名的edit获得焦点,即edit1.setfocus,让用户再次输入,但我的程序setfocus不起作用了,程序不理会setfocus,直接运行下边的代码了。是何原因呢?

解决方案 »

  1.   

    edit1.setfocus只是设定焦点(当前ButtonClick事件程序运行完后才起作用),可以在其后加上
    exit;
    跳出,不运行下面的代码
      

  2.   

    procedure TfmLogin.btnOkClick(Sender: TObject);
    var
      sConnectionString, sSqlString: string;
    begin
      if Length(Trim(cbSqlServerName.Text)) <> 0 then
        if Length(Trim(edtUserName.Text)) <> 0 then
          begin
            fmLogin.Cursor := crHourGlass;        sConnectionString := 'Provider=SQLOLEDB.1;Persist Security Info=True;User ID=' + Trim(edtUserName.Text) + ';Initial Catalog=Northwind;Data Source=' + cbSqlServerName.Text + ';Password=' + edtPassword.Text;        dmAllWeb.ADOConnection1.ConnectionString := sConnectionString;        try
              dmAllWeb.ADOConnection1.Connected := True;          with dmAllWeb.ADOStoredProc1 do
              begin
                Close;
                ProcedureName:='LoginInformation';            parameters.Clear;
                Parameters.Refresh();            Parameters.CreateParameter('@ClientName',ftstring,pdInput,20,0);
                Parameters.CreateParameter('@Password',ftstring,pdInput,20,0);
                Parameters.CreateParameter('@MachineId',ftstring,pdInput,23,0);
                Parameters.CreateParameter('@VersionNum',ftstring,pdInput,13,0);
                Parameters.CreateParameter('@LoginMessage',ftinteger,pdOutput,1,0);
                Prepared;            Parameters.ParamByName('@ClientName').Value := Trim(edtUserName.Text);
                Parameters.ParamByName('@Password').Value := Trim(edtPassword.Text);
                Parameters.ParamByName('@MachineId').Value := MachineID1.GetMachineID;
                Parameters.ParamByName('@VersionNum').Value := Label7.Caption;            ExecProc;            case Parameters.ParamByName('@LoginMessage').Value of
                  0: begin
                       Application.MessageBox('用户不存在!','错误', MB_OK or MB_ICONERROR);
                       edtUserName.SetFocus;
                       exit;
                     end;
                  1: begin
                       Application.MessageBox('密码错误!请重新输入密码!','错误', MB_OK or MB_ICONERROR);
                       edtUserName.SetFocus;
                       exit;
                     end;
                  2: begin                 end;
                  3: begin
                       Application.MessageBox('该用户已注册在另一台电脑上!','错误', MB_OK or MB_ICONERROR);
                       edtUserName.SetFocus;
                     end;
                  4: begin
                     end;
                  5: begin
                       fmAllWeb.MenuImorptData.Visible := true;
                       fmAllWeb.MenuAddClient.Visible := true;
                     end;
                  6: begin
                       Application.MessageBox('该帐号被锁定!','错误', MB_OK or MB_ICONERROR);
                       edtUserName.SetFocus;
                     end;
                end;          end;
            except
              on e: EOleException do
              begin
                dmAllWeb.ADOConnection1.Connected := False;
                Application.MessageBox(pchar(E.Message),'错误', MB_OK or MB_ICONERROR);
              end;
          end;
        end
        else
        begin
          Application.MessageBox('请输入帐号!','错误', MB_OK or MB_ICONERROR);
          edtUserName.SetFocus;
          exit;
        end
      else
      begin
        Application.MessageBox('登陆服务器必须输入!','错误', MB_OK or MB_ICONERROR);
        cbSqlServerName.SetFocus;
        exit;
      end;
      

  3.   

    你这段代码可以不用加exit
    刚才试了一下,edtUserName.SetFocus没错,是按照程序运行的,没有出现你说的问题
    你设断点,F8单步调试一下,看看是不是其他原因
      

  4.   

    我的程序是mdi窗体呢,在我这setfocus是不行的。怪了。