重复登录窗体的实现(非原创)

之前自己一直是在loginform窗体中写判断语句,用户名及密码有错的话,不能重新显示该窗体。

参考其他网友的做法,在主窗体的oncreate事件中动态创建loginform.并在Mainform中的oncreate事件来判断登录的正确性。可以重复调用loginform.showmodal事件。

procedure TForm1.check_psw(times: integer);
begin
  inc(times);
  
if (loginfrm.ShowModal=mrok) then
    
begin
      
if  (times<4then
        
begin
             
if (loginfrm.Edit1.text<>'Edit1'then  //此处省略了从数据库中查询用户名及密码的过程
                
begin
                  application.MessageBox(
'没有该用户','Error',0);
                  check_psw(times);
                
end
                
else
                   
if (loginfrm.Edit2.text<>'12'then
                      
begin
                        application.MessageBox(
'密码错误','Error',0);
                        check_psw(times);
                      
end
                   
else
                     loginfrm.Free;
          
end
      
else
         
begin
            application.MessageBox(
'over 3 times','error',0);
            application.Terminate;
            loginfrm.Free;
          
end;
     
end
       
else
           
begin
            application.MessageBox(
'退出程序','info',0);
            application.Terminate;
            loginfrm.Free;
          
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  loginfrm:
=Tloginform.Create(nil);
  check_psw(
0);
end;

 以上想法参照网友,非自己原创。但找不到出处了。个别处理自己作出了调整!

原文地址:https://www.cnblogs.com/samsonleung/p/1247179.html