求一個有三次限制登錄的登錄窗口代碼?
procedure Tdenglufrm.Button1Click(Sender: TObject);
var
username ,password :string;
begin
if edit1.Text=''then
messagebox(denglufrm.handle,'请输入用户名','警告',mb_ok+mb_iconwarning)
else
if edit2.Text=''then
messagebox(denglufrm.handle,'请输入密码','警告',mb_ok+mb_iconwarning)
else
begin
username:=edit1.Text;
password:=edit2.Text;
with ADOQuerydl do
begin
close;
sql.clear;
sql.add('select * from xtglry where yh=');
sql.add(''''+username+''''+'and mm='+''''+password+'''');
open;
first;
if recordcount=1 then
begin
denglufrm.Hide;
MainFrm.Show;
end
else
messagebox(denglufrm.handle ,'请输入正确的密码','警告',mb_ok+mb_iconwarning);
edit1.Text:='';
edit2.Text:=''
end;
end;
end;
这个是我的代码,我想添加循环来控制登录三次就退出,应该怎样设置啊?
[解决办法]
i_Times: integer = 3
procedure Tdenglufrm.Button1Click(Sender: TObject);
var
username ,password :string;
begin
if edit1.Text= ' 'then
messagebox(denglufrm.handle, '请输入用户名 ', '警告 ',mb_ok+mb_iconwarning)
else
if edit2.Text= ' 'then
messagebox(denglufrm.handle, '请输入密码 ', '警告 ',mb_ok+mb_iconwarning)
else
begin
if i_Times = 0 then
Application.terminate;
username:=edit1.Text;
password:=edit2.Text;
with ADOQuerydl do
begin
close;
sql.clear;
sql.add( 'select * from xtglry where yh= ');
sql.add( ' ' ' '+username+ ' ' ' '+ 'and mm= '+ ' ' ' '+password+ ' ' ' ');
open;
first;
if recordcount=1 then
begin
denglufrm.Hide;
MainFrm.Show;
end
else
begin
messagebox(denglufrm.handle , '请输入正确的密码 ', '警告 ',mb_ok+mb_iconwarning);
edit1.Text:= ' ';
edit2.Text:= ' ';
Dec(i_Times,1);
end;
end;
end;
end;
[解决办法]
增加一个全局变量,每登录一次就开始判断
var
LoginCount:byte;
procedure Tdenglufrm.Button1Click(Sender: TObject);
var
username ,password :string;
begin
if loginCount>3 then
exit;//大于三次退出
if edit1.Text= ' 'then
messagebox(denglufrm.handle, '请输入用户名 ', '警告 ',mb_ok+mb_iconwarning)
else
if edit2.Text= ' 'then
messagebox(denglufrm.handle, '请输入密码 ', '警告 ',mb_ok+mb_iconwarning)
else
begin
username:=edit1.Text;
password:=edit2.Text;
with ADOQuerydl do
begin
close;
sql.clear;
sql.add( 'select * from xtglry where yh= ');
sql.add( ' ' ' '+username+ ' ' ' '+ 'and mm= '+ ' ' ' '+password+ ' ' ' ');
open;
first;
if recordcount=1 then
begin
denglufrm.Hide;
MainFrm.Show;
end
else
begin
inc(LoginCount);//错误的时候增加一
messagebox(denglufrm.handle , '请输入正确的密码 ', '警告 ',mb_ok+mb_iconwarning);
edit1.Text:= ' ';
edit2.Text:= ' ';
end;
end;
end;
end;
[解决办法]
增加一个全局变量,每登录一次就开始判断
var
LoginCount:byte;
在form初始化的时候:
procedure TForm.creat(Sender: TObject);
begin
LoginCount:=0;
end;
procedure Tdenglufrm.Button1Click(Sender: TObject);
var
username ,password :string;
begin
if loginCount >3 then
exit;//大于三次退出
if edit1.Text= ' 'then
messagebox(denglufrm.handle, '请输入用户名 ', '警告 ',mb_ok+mb_iconwarning)
else
if edit2.Text= ' 'then
messagebox(denglufrm.handle, '请输入密码 ', '警告 ',mb_ok+mb_iconwarning)
else
begin
username:=edit1.Text;
password:=edit2.Text;
with ADOQuerydl do
begin
close;
sql.clear;
sql.add( 'select * from xtglry where yh= ');
sql.add( ' ' ' '+username+ ' ' ' '+ 'and mm= '+ ' ' ' '+password+ ' ' ' ');
open;
first;
if recordcount=1 then
begin
denglufrm.Hide;
MainFrm.Show;
end
else
begin
LoginCount++;//错误的时候增加一
messagebox(denglufrm.handle , '请输入正确的密码 ', '警告 ',mb_ok+mb_iconwarning);
edit1.Text:= ' ';
edit2.Text:= ' ';
end;
end;
end;
end;
[解决办法]
function errors(count:integer):integer; //自定义一个函数errors(),返回整数值,为登录出错次数
var
x:integer;
begin
x:=count+1;
if(x<3) then //出错3次拒绝登录
showmessage('请输入正确的用户名/密码。'+#13#13+'您只有三次机会。') //前两次输入错误只警告
else
showmessage('对不起,您不能登录!'); //事不过三
errors:=x;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Edit1.Text:=Trim(Edit1.Text); //用户名:去除前导和尾随空格
Edit2.Text:=Trim(Edit2.Text); //密码:同上
if((Edit1.Text<>'') and (Edit2.Text<>'')) then //若用户名和密码都已输入
begin
//查询数据库,自己写
//...
if (..) then //若查询结果非空,允许登录
begin
denglufrm.Hide;
MainFrm.Show;
end
else
i:=errors(i); //若查询结果为空,即为出错,调用自定义函数errors()
if (i>=3) then //如果是第三次出错
//...退出程序,自己写
end
else
showmessage('请输入用户名/密码'); //若未输入用户名或未输入密码
end;