我的登錄窗口在點擊x來關閉時,竟然主窗口也會出來。為什麼呢,怎樣改進?在主程式裡是這樣寫的:begin
Application.Initialize;
Application.CreateForm(TDataModule1, DataModule1);
Application.CreateForm(TMainForm, MainForm);//主窗口
Application.CreateForm(TLoginForm, LoginForm); //登錄窗口
LoginForm.ShowModal;
LoginForm.Free;
Application.Run;
end.在登錄窗口是這樣寫的:
//點擊事件
procedure TLoginForm.Button1Click(Sender: TObject);
begin
ADOQuery1.Close;
ADOQuery1.Parameters.ParamByName('Param1').Value:=Edit1.Text;
ADOQuery1.Parameters.ParamByName('Param2').Value:=Edit2.Text;
ADOQuery1.Open; if (ADOQuery1.FieldByName('userid').Value=Edit1.Text) and (ADOQuery1.FieldByName('password').Value=Edit2.Text) then
Close
else
ShowMessage('帳號或密碼錯誤哦!')
end;
Application.Initialize;
Application.CreateForm(TDataModule1, DataModule1);
Application.CreateForm(TMainForm, MainForm);//主窗口
Application.CreateForm(TLoginForm, LoginForm); //登錄窗口
LoginForm.ShowModal;
LoginForm.Free;
Application.Run;
end.在登錄窗口是這樣寫的:
//點擊事件
procedure TLoginForm.Button1Click(Sender: TObject);
begin
ADOQuery1.Close;
ADOQuery1.Parameters.ParamByName('Param1').Value:=Edit1.Text;
ADOQuery1.Parameters.ParamByName('Param2').Value:=Edit2.Text;
ADOQuery1.Open; if (ADOQuery1.FieldByName('userid').Value=Edit1.Text) and (ADOQuery1.FieldByName('password').Value=Edit2.Text) then
Close
else
ShowMessage('帳號或密碼錯誤哦!')
end;
Application.Terminate;另外,主窗体最好不要设为自动创建!!!
可以啊,你認為怎樣做好?我用Delphi不久,請多多指教!謝謝!
begin
ADOQuery1.Close;
ADOQuery1.Parameters.ParamByName('Param1').Value:=Edit1.Text;
ADOQuery1.Parameters.ParamByName('Param2').Value:=Edit2.Text;
ADOQuery1.Open; if (ADOQuery1.FieldByName('userid').Value=Edit1.Text) and (ADOQuery1.FieldByName('password').Value=Edit2.Text) then
begin
Close;
end
else
begin
ShowMessage('帳號或密碼錯誤哦!')
Application.Terminate;
end;end;
CanClose := False;
至于解决方法。
在登录窗体中加个Private变量 LoginSuccess: Boolean;
在Login窗体的OnCloseQuery事件中,加
CanClose := False;
CanCLose := LoginSuccess;然后再把上一段代码改为
procedure TLoginForm.Button1Click(Sender: TObject);
begin
ADOQuery1.Close;
ADOQuery1.Parameters.ParamByName('Param1').Value:=Edit1.Text;
ADOQuery1.Parameters.ParamByName('Param2').Value:=Edit2.Text;
ADOQuery1.Open; if (ADOQuery1.FieldByName('userid').Value=Edit1.Text) and (ADOQuery1.FieldByName('password').Value=Edit2.Text) then
begin
LoginSuccess := True;//新加的!
Close;
end
else
begin
ShowMessage('帳號或密碼錯誤哦!')
Application.Terminate;
end;end;
1,點擊LoginForm的x後關不掉LoginForm這個窗口;
2,加了下面的代碼Application.Terminate後,登錄失敗後將會關閉LoginForm窗口
begin
ShowMessage('帳號或密碼錯誤哦!')
Application.Terminate;
end;
2,加了下面的代碼Application.Terminate後,登錄失敗後將會關閉LoginForm窗口
begin
ShowMessage('帳號或密碼錯誤哦!')
Application.Terminate;
end;