一个初手,做了一个需要密码验证的窗口,可是在登录窗口就算密码输入错误仍旧能进入主窗体。在网上对照其他高人做的登录窗口,没看出问题所在,请高人给予讲解。
下面是登录和工程文件。program Project1;uses
Forms,
UMain in 'UMain.pas' {main},
UDM in 'UDM.pas' {DM: TDataModule},
USC in 'USC.pas' {SC},
SysUtils,
Dialogs,
Controls,
ULOGIN in 'ULOGIN.pas' {LOGIN};{$R *.res}
begin
application.Initialize;
Application.CreateForm(Tmain, main);
Application.CreateForm(TDM, DM);
Application.CreateForm(TLOGIN,LOGIN);
LOGIN.ShowModal;
LOGIN.Free;
Application.Run;end.登录窗体的登录按钮:
procedure TLOGIN.BitBtn1Click(Sender: TObject);
begin
Inc(iTimes);
if Trim(password.Text) = dm.Qoper.FieldByName('pas').AsString then
begin
login.Close;
end
else
begin
Messagebox(0,'密码错误,请重新登录!'+#13#10+'并请确定你是否有进入本系统的权限。','',MB_OK+MB_ICONSTOP);
// application.Terminate; //加入这行的意思是实在没办法了,只能强行终止程序。不加这个语句主窗体依然可以进入。
end; if iTimes = 4 then
begin
application.MessageBox('您进行系统身份确认程序错误已经超过3次,本系统将停止执行。','警告',0);
application.Terminate;
end;
end;
下面是登录和工程文件。program Project1;uses
Forms,
UMain in 'UMain.pas' {main},
UDM in 'UDM.pas' {DM: TDataModule},
USC in 'USC.pas' {SC},
SysUtils,
Dialogs,
Controls,
ULOGIN in 'ULOGIN.pas' {LOGIN};{$R *.res}
begin
application.Initialize;
Application.CreateForm(Tmain, main);
Application.CreateForm(TDM, DM);
Application.CreateForm(TLOGIN,LOGIN);
LOGIN.ShowModal;
LOGIN.Free;
Application.Run;end.登录窗体的登录按钮:
procedure TLOGIN.BitBtn1Click(Sender: TObject);
begin
Inc(iTimes);
if Trim(password.Text) = dm.Qoper.FieldByName('pas').AsString then
begin
login.Close;
end
else
begin
Messagebox(0,'密码错误,请重新登录!'+#13#10+'并请确定你是否有进入本系统的权限。','',MB_OK+MB_ICONSTOP);
// application.Terminate; //加入这行的意思是实在没办法了,只能强行终止程序。不加这个语句主窗体依然可以进入。
end; if iTimes = 4 then
begin
application.MessageBox('您进行系统身份确认程序错误已经超过3次,本系统将停止执行。','警告',0);
application.Terminate;
end;
end;
ado.sql.add("sql语句");
ado.open;
if ado.eof then
begin
showmessage("密码错误");
end
else
begin
show.主窗体;
登陆窗体.hide;
end
ado.sql.add("sql语句");
ado.open;
if ado.isempty then
begin
showmessage("密码错误");
exit;
end
else
begin
主窗体.xclose=1;
登陆窗体.close;
end;
主窗体中相应代码:
var
xclose:integer;
...
主窗体.formshow(sender:tobject);
begin
xclose:=0;
登陆窗体:=Tlogin.create(self); //Tlogin即为登陆窗体类型
登陆窗体.showmodule;
登陆窗体.free;if xclose=0 then
application.Terminate;
...
你可以在
begin
ado.sql.add("sql语句");
ado.open;
if ado.isempty then
begin里面的 "SQL语句"
里写成: select * from logininfo where username='''+edituser.text+''' and pwd='''+pwd.text+''' 即可.这样可以省很多启动时间.