主窗体与登陆窗体的问题(100分)

  • 主题发起人 主题发起人 wapftgdss
  • 开始时间 开始时间
W

wapftgdss

Unregistered / Unconfirmed
GUEST, unregistred user!
orm1是主窗体,form2是登陆窗体,程序运行后,本应该先由登陆窗体判断用户和密码
是否正确,如果正确就进入主窗体,同时关闭登陆窗体。如果错误就重新输入,
有三次机会,三次输入错误就自动退出。
但现在的情况是程序运行后主窗体与登陆窗体同时可见,登陆窗体在主窗体的上面,
但可以直接对主窗体进行操作,无须登陆窗体的认证。
请各位帮帮忙,给予指正。以下是部分程序代码。

主窗体form1的FormShow事件:
procedure TForm1.FormShow(Sender: TObject);
begin
application.CreateForm(tform2,form2);
Form2.ShowModal;
end;





登陆窗体form2的全部代码:
unit Unit2;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, jpeg, ExtCtrls;

type
TForm2 = class(TForm)
edit1: TEdit;
edit2: TEdit;
BitBtn1: TBitBtn;
BitBtn2: TBitBtn;
Image1: TImage;
procedure BitBtn1Click(Sender: TObject);
procedure BitBtn2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
private
{ Private declarations }
public

{ Public declarations }
end;

var
Form2: TForm2;
n:integer;
FormCanClose:Boolean;
implementation

uses Unit3,unit1;

{$R *.dfm}

procedure TForm2.BitBtn1Click(Sender: TObject);
//登陆窗体的确定按纽事件
begin
if dm.Tadopassword.FieldByName('name').Text<>edit1.Text then
begin
n:=n-1;
Messagebox(handle,pchar('用户名错误,还有'+inttostr(n)+'次机会!'),'警告!',MB_ICONWARNING);
if n=0 then exit;
edit1.Clear;
edit2.Clear;
end
else if dm.Tadopassword.FieldByName('key').Text<>edit2.Text then
begin
n:=n-1;
Messagebox(handle,pchar('密码错误,还有'+inttostr(n)+'次机会!'),'警告!',MB_ICONWARNING);
if n=0 then exit;
edit2.Clear;
end
else
begin
//close;
FormCanClose := True;
form1.show;
end;
end;

procedure TForm2.BitBtn2Click(Sender: TObject);
//登陆窗体的取消按纽事件
begin
close;
end;

procedure TForm2.FormCreate(Sender: TObject);
begin
n:=3;
FormCanClose:=false;
end;

procedure TForm2.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
canclose:=FormCanClose;
end;

end.
 
你在登入电脑时自己开启为FROM2,把FROM1入在未激活区里,
通过认证后再开启FROM1就好了,不要把FROM1和FROM2同时放在ACTIVE FROM 里
 
需要在program里面修改登录窗体和主窗体的出现顺序,手动调整一下里面的代码,然后对登录窗体和主窗体都采用动态创建的方式就好了!
 
最好的办法是修改工程文件,
如下:
begin
Application.Initialize;
login_frm:=tlogin_frm.Create(application);//建立登陆框
login_frm.showmodal;显示
if **** then //****为LOGIN中一全局布尔值变量
begin
logo_frm:=tlogo_frm.create(application);//欢迎界面
logo_frm.show;//显示
logo_frm.Update;//更新
Application.Title := '标题';
Application.CreateForm(TMAIN_FRM, MAIN_FRM);//建立主窗体
logo_frm.Close ;//关闭欢迎窗体
login_frm.close;
Application.Run;
end ELSE
BEGIN
login_frm.close;
Application.Run;
END;
end.
 
你要在工程文件那里去,先创建登陆窗体,然后在做处理…………
 
我前不久也遇到这样的问题,我是这样解决的。(Frmain为主窗体,Frm_login为登录窗体)
主窗体代码如下:
procedure TFrmain.FormShow(Sender: TObject);
begin
Application.CreateForm(TFrm_login,Frm_login); //动态创建窗体
with Frm_login do
try
ShowModal;
finally
Free;
end;
//ShowMessage(N_gxdw) ;
end;
子窗体代码为:我在程序开头设置了一个变量:
var
Frm_login: TFrm_login;
M_AppTerminate: boolean;
并设置一个计数变量:
private
count : Integer ; //初始化 计数器变量
procedure TFrm_login.FormShow(Sender: TObject); //此子窗体显示时,需从相关表中提取合法用户。
begin
...
M_AppTerminate := true;
count := 0 ; //计数器变量 赋 初值
end;

//下面事件为判断是否是合法用户及登录次数。
procedure TFrm_login.EdpassKeyPress(Sender: TObject; var Key: Char);
begin
if (Key=#13) then
begin
count := count + 1 ;
//Showmessage(InttoStr(count)) ;
ADOQ1.Close;
ADOQ1.SQL.Clear;
ADOQ1.SQL.Add(' Select * From kfsj_user where user_id = '''+Trim(Cbuser.Text)+''' and user_password = '''+Trim(Edpass.Text)+''' ') ;
ADOQ1.Active:= True;
if ADOQ1.RecordCount=0 then
begin
if count <> 4 then
begin
Application.MessageBox('你输入的密码不正确,请重新输入','警告',16) ;
Edpass.Text:= '' ;
Edpass.SetFocus ;
Exit ;
end
else
Application.Terminate;
end
else
begin
if Trim(Cbgxdw.Text)='' then
Application.MessageBox('管辖单位不能为空,请重新选择','警告',48)
else
begin
ADOQ1.Close ;
M_AppTerminate := False ;
Frm_Login.Close ;
end;
end;
end;
end;

希望对你会有所帮助。
 
改工程文件
Application.Initialize;
Application.CreateForm(TMAINForm, MAINForm);//主
Application.CreateForm(TFMLoad, FMLoad);//登
FMLoad.ShowModal ;//加上这句
Application.Run;
 
to jzg007,你的方法不行,还是同样的问题
 
Application.Initialize;
with TFMLoad.Create(nil) do if ShowModal <> mrOk then Exit;
Application.CreateForm(TMAINForm, MAINForm);//主
Application.Run;
你在TFMLoad上面放两个按钮,一个的ModalResult设置成mrOK,另外一个ModalResult设置成mrCancel,你分别点击两个试试看.这里用ModalResult只是演示,你可以自定义一个窗体变量,或者自己写一个表达式,来完成登陆与否的判断.如:
Application.Initialize;
with TFMLoad.Create(nil) do
begin
ShowModal;
if edtUserPassword.Text <> '1234' then Exit;
end;
Application.CreateForm(TMAINForm, MAINForm);
Application.Run;
 
to weiliu, 按你的放法改了工程文件,程序运行后,点击登陆窗体上的确定按纽时就出现一个对话框提示:Access violation at address 004CD2D1 in module 'project1.exe',read of address 0000005C
我改的工程文件代码如下,不知道是不是改错了,请指教!

工程文件:
begin
Application.Initialize;
form2:=TForm2.Create(application);
form2.Showmodal;
//form2.Update;
if **** then
begin
Application.Title := '我的电话簿';
Application.CreateForm(TForm1, Form1);
form2.Close;
Application.Run;
end
else
begin
form2.Close;
Application.Run;
end;
end.
然后去掉主窗体form1的FormShow事件,
将登陆窗体form2的代码改为:
var
Form2: TForm2;
n:integer;
****:boolean;
//FormCanClose:Boolean;
implementation

uses Unit3,unit1;

{$R *.dfm}

procedure TForm2.BitBtn1Click(Sender: TObject);
begin
if dm.Tadopassword.FieldByName('name').Text<>edit1.Text then
begin
n:=n-1;
Messagebox(handle,pchar('用户名错误,还有'+inttostr(n)+'次机会!'),'警告!',MB_ICONWARNING);
if n=0 then application.Terminate;
edit1.Clear;
edit2.Clear;
end
else if dm.Tadopassword.FieldByName('key').Text<>edit2.Text then
begin
n:=n-1;
Messagebox(handle,pchar('密码错误,还有'+inttostr(n)+'次机会!'),'警告!',MB_ICONWARNING);
if n=0 then application.Terminate;
edit2.Clear;
end
else
begin
****:=true;
form1.show;
self.Hide;
end;
end;

procedure TForm2.BitBtn2Click(Sender: TObject);
begin
close;
end;

procedure TForm2.FormCreate(Sender: TObject);
begin
n:=3;
****:=false;
end;

作了这些工作以后,出现我上述的错误,请问还有哪里不对?
 
to zqw0117,你的方法还是不行
 
我的方法就是3次机会没有说而已啊,楼主看来一点都不会变通,必须要人家都一字不漏的说。。。。。。。

你的登陆窗口判断用户输入的密码是否正确,超过三次就关闭,如果正确赋值一个全局变量为True,或者自己窗体拥有的变量为True,外部再来判断。

type
LoginForm = class (TForm)
///....省略
private
FWrongInputTime: Integer;
FPasswordRight: Boolean;
public
property PasswordRight: Boolean read FPasswordRight;
end;

在你的确定按钮中判断用户输入的密码是否正确,如果正确,设置FPasswordRight := True,然后close窗口;如果不正确,设置FPasswordRight := False,同时给FWrongInputTime加1,接着判断FWrongInputTime是否>=3,如果>=3则直接Close(这时FPasswordRight=False)。外部代码如下:
Application.Initialize;
with TFMLoad.Create(nil) do
begin
try
ShowModal;
if PasswordRight = False then Exit;
finallly
Free;
end;
end;
Application.CreateForm(TMAINForm, MAINForm);
Application.Run;
 
本人回答问题只提供方案
1、在工程选项里去掉自动创建的登录窗口
2、在主窗口的oncreate事件中创建登录窗口并showmodal
3、在登录窗口验证用户名密码,并将结果反馈给主窗口
4、主窗口在登录窗口销毁后判断验证结果
 
to zqw0117,我照你说的方法改了,出现的错误和我在回复to weiliu,时提到的错误一模一样,点击登陆窗体上的确定按纽时就出现一个对话框提示:Access violation at address 004CD2D1 in module 'project1.exe',read of address 0000005C
我想是我太笨了,这么多的老师也教不好我这个傻学生.
我改的工程文件如下:
begin
Application.Initialize;
with Tform2.Create(nil) do
begin
try
ShowModal;
if PasswordRight = False then Exit;
finally
Free;
end;
end;

Application.Title := '我的电话簿';
Application.CreateForm(TForm1, Form1);
Application.Run;
end.

登陆窗口改为如下:
type
TForm2 = class(TForm)
edit1: TEdit;
edit2: TEdit;
BitBtn1: TBitBtn;
BitBtn2: TBitBtn;
Image1: TImage;
procedure BitBtn1Click(Sender: TObject);
procedure BitBtn2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
n: Integer;
FPasswordRight: Boolean;
{ Private declarations }
public
property PasswordRight: Boolean read FPasswordRight;
{ Public declarations }
end;

var
Form2: TForm2;


implementation

uses Unit3, Unit1;

{$R *.dfm}

procedure TForm2.BitBtn1Click(Sender: TObject);
begin
if dm.Tadopassword.FieldByName('name').Text<>edit1.Text then
begin
FPasswordRight := False;
n:=n-1;
Messagebox(handle,pchar('用户名错误,还有'+inttostr(n)+'次机会!'),'警告!',MB_ICONWARNING);
if n=0 then self.close;
edit1.Clear;
edit2.Clear;
end
else if dm.Tadopassword.FieldByName('key').Text<>edit2.Text then
begin
FPasswordRight := False;
n:=n-1;
Messagebox(handle,pchar('密码错误,还有'+inttostr(n)+'次机会!'),'警告!',MB_ICONWARNING);
if n=0 then self.close;
edit2.Clear;
end
else
begin
FPasswordRight := True;
close;
end

end;

procedure TForm2.BitBtn2Click(Sender: TObject);
begin
close;
end;

procedure TForm2.FormCreate(Sender: TObject);
begin
n:=3;
end;
 
将工程文件改为
begin
Application.Initialize;

Application.Title := '我的电话簿';
Application.CreateForm(TForm1, Form1);
Application.ShowMainForm := False;//隐藏主窗口
with Tform2.Create(nil) do
begin
try
ShowModal;
if PasswordRight = False then
Application.Terminate//如果密码错误终止程序运行
else
Form1.Show;
finally
Free;
end;
end;
Application.Run;
end.
 
今天终于弄好了!非常感谢各位的指点,尤其要感谢zqw0117,虽然我最后实现的方法跟你教的不一样,我最后没有修改工程文件。我只修改了form1和form2.
 
后退
顶部