登录窗体中的label3在edit1回车后显示登录人员的姓名,checkbox1用来保存密码,但是我的程序在执行以下代码后,当密码错误时,才显示登录人员的姓名,正确的话就直接进入主程序了,求解?
procedure Tform.b_loginClick(Sender: TObject);
begin
try
adoconnection1.Connected:=true;
t_user.Filter:='rybh='''+ edit1.Text+'''';
t_user.Filtered:=true;
t_user.Open;
except
application.MessageBox('数据库连接错误!','请与管理员联系!',MB_OK);
application.Terminate;
end;
if t_user.RecordCount=1 then
begin
Label3.Caption := t_user['xm'];
if t_user['dlmm']=trim(edit2.Text) then
begin
edit2.Text:=trim(edit2.Text);
// t_user['dlmm']:=trim(t_user['dlmm']);
begin
if CheckBox1.Checked then
begin
with TIniFile.Create(ChangeFileExt(Application.ExeName, '.INI')) do
try
WriteString('Common', 'User', Edit1.Text);
WriteString('Common', 'Pass', Edit2.Text);
WriteBool('Common', 'Auto', CheckBox1.Checked);
finally
Free;
end;
end;
end;
form.Hide; form2.Show ;
end
else
application.MessageBox('密码错误','请确认密码!',MB_OK);
end
else
application.MessageBox('无此用户!','',MB_OK);
end;
procedure Tform.b_loginClick(Sender: TObject);
begin
try
adoconnection1.Connected:=true;
t_user.Filter:='rybh='''+ edit1.Text+'''';
t_user.Filtered:=true;
t_user.Open;
except
application.MessageBox('数据库连接错误!','请与管理员联系!',MB_OK);
application.Terminate;
end;
if t_user.RecordCount=1 then
begin
Label3.Caption := t_user['xm'];
if t_user['dlmm']=trim(edit2.Text) then
begin
edit2.Text:=trim(edit2.Text);
// t_user['dlmm']:=trim(t_user['dlmm']);
begin
if CheckBox1.Checked then
begin
with TIniFile.Create(ChangeFileExt(Application.ExeName, '.INI')) do
try
WriteString('Common', 'User', Edit1.Text);
WriteString('Common', 'Pass', Edit2.Text);
WriteBool('Common', 'Auto', CheckBox1.Checked);
finally
Free;
end;
end;
end;
form.Hide; form2.Show ;
end
else
application.MessageBox('密码错误','请确认密码!',MB_OK);
end
else
application.MessageBox('无此用户!','',MB_OK);
end;
难道密码不用输入的吗 ?我见过用combobox保存用户名的。其实最简单省事的就是两个 edit,我就是这样做的。
t_user.Filter:='rybh='''+ edit1.Text+'''';
t_user.Filtered:=true;
t_user.Open;
这是用户名的呢
不能寫在onclick中
因為寫在這里,你還沒看清form就已經hide了
最好是寫在edit1.onexit中,用戶名正確就賦值
adoconnection1.Connected:=true;
t_user.Filter:='rybh='''+ edit1.Text+'''';
t_user.Filtered:=true;
.........
以下是我对这代码的理解:
//1。查该用户是否存在:
//if user 存在:
//if 该用户对应的密码是否为checkbox是否以前已经打钩。
如果是,则直接hide LoginForm;
否则查该密码是否对上,如果对上,
begin
if checkbox.check=true then
begin
//写入ini问件,下次方便访问;
end;
Login_From.Hide;
Form2.show;
end else
begin
showMessage('密码输入出错');
end;
//这样理解可以?
begin
Label3.Caption := t_user['xm'];
if t_user['dlmm']=trim(edit2.Text) then
begin
edit2.Text:=trim(edit2.Text);
..........................
.................
form.Hide; form2.Show ;
當用戶名和口令都正確後,程序就hide當前窗體,然後顯示 form2,form都hide了,怎麼看得見 label3上的xm ?
因此,建議樓主把姓名之類顯示在form2上,比如作一個status(狀態條) 放到窗體下面.另外,樓主的代碼很不整齊,讓人看起來很吃力/
我給你一點提示.
1用戶名一定是唯一的。
那麼你可以用一個adodataset來查詢此用戶名,如果找到了記錄,就以
if adodataset1.fieldbyname('password').asstring= edit口令.text then
showmessage('passed ok')
else
showmessage('passed false ');要注意的是,在檢測用戶名或口令前,先判斷它們是否為空,如果是空的就不允許查詢。請樓主至少給我90分,好嗎》
http://www.tanyong.com
procedure TFormlogin.Edit1Exit(Sender: TObject);
begin
adodm.userquery.Close;
adodm.userquery.SQL.Clear;
adodm.userquery.SQL.Add('select * from usertable where 用户编号='+''''+trim(edit1.Text)+'''');
adodm.userquery.Open;
edit2.Text:=adodm.userquery.FieldbyName('用户姓名').AsString;
if (adodm.userquery.Eof=adodm.userquery.Bof) then
beginshowmessage('没有这个用户');
edit1.Text:='';
edit1.SetFocus;
end
elsebitbtn1.Enabled:=true;
end;procedure TFormlogin.BitBtn1Click(Sender: TObject);
var mm:string;
begin
mm:=trim(adodm.userquery.fieldbyName('用户密码').AsString);
if (mm=trim(edit3.Text)) then
begin
form1.Show;
formlogin.Visible:=false;end
else
begin
showmessage('密码输入错误!');
edit3.Text:='';
edit3.SetFocus;
end
end;procedure TFormlogin.FormShow(Sender: TObject);
begin
bitbtn1.Enabled:=false;
end;procedure TFormlogin.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if (key=#13) then
edit3.SetFocus;
end;procedure TFormlogin.Edit3KeyPress(Sender: TObject; var Key: Char);
begin
if (key=#13) then
bitbtn1.SetFocus;
end;procedure TFormlogin.BitBtn2Click(Sender: TObject);
begin
application.Terminate;
end;