小弟做毕业设计被这个问题卡住了,眼看答辩就要来了,还是解决不了请大侠们出手相助,感激涕零~unit1是个持续2秒钟欢迎信息显示窗体,在运行代码时候,登录窗体总是一闪而过,自动退出了,不知道什么原因,几乎把下面2段代码调遍了也没找出个所以然,请大侠帮个忙,先说谢谢!
登录窗体代码
unit Unit2;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, DBTables, StdCtrls,ExtCtrls;type
TFormLogin = class(TForm)
Edit1: TEdit;
ComboBox1: TComboBox;
Button1: TButton;
DataSource1: TDataSource;
Query1: TQuery;
Database1: TDatabase;
Label1: TLabel;
Label2: TLabel;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations } public
{ Public declarations } end;var
FormLogin: TFormLogin;
UserID:string;
count:integer; //登录次数
ok:boolean;
implementationuses Unit_FormMain;{$R *.dfm}procedure TFormLogin.Button1Click(Sender: TObject);
begin
count:=0;
ok:=false;
while ok do
begin
//判断用户输入的工号是否为空
if FormLogin.ComboBox1.Text='' then
begin
ShowMessage('工号不能为空!');
Exit;
end;
if FormLogin.Edit1.Text='' then
begin
ShowMessage('密码不能为空');
Edit1.SetFocus;
Exit;
end;
if (FormLogin.ComboBox1.Text<>'')and (FormLogin.Edit1.Text<>'') then
begin
while count<3 do
begin
FormLogin.Query1.close;
FormLogin.Query1.sql.clear;
FormLogin.Query1.sql.add('select * from OperInfo where ID='''+Formlogin.ComboBox1.Text+'''');
FormLogin.Query1.open;
if FormLogin.Query1.recordcount=0 then
begin
inc(count);
if count>=3 then
begin
messagedlg('您已经登录失败超过3次,程序将终止',mtWarning,[mbok],0);
close;
application.Terminate;
end else
begin
Showmessage('工号或密码不正确!');
Exit;
//self.ModalResult:=mrcancel;
//FormLogin.Query1.close;
Edit1.Text:='';
Edit1.SetFocus
end;
end;
if FormLogin.Query1.recordcount>0 then
begin
ok:=true;
end; end;
end;
end;
end;procedure TFormLogin.Button2Click(Sender: TObject);
begin
application.Terminate;
end;procedure TFormLogin.FormCreate(Sender: TObject);
begin
//判断Query1中的当前记录是否是最后一个记录
while not Query1.Eof do
begin
//将当前纪录值添加到ComboBox1组合框中
ComboBox1.AddItem(Query1.FieldValues['ID'],sender);
//将当前记录下移一个
Query1.MoveBy(1);
end;
end;end.
主窗体dpr代码
program Project_FormMain;uses
Forms,
Controls,
Unit_FormMain in 'Unit_FormMain.pas' {FormMain},
Unit1 in 'Unit1.pas' {FormSplash},
Unit2 in 'Unit2.pas' {FormLogin};{$R *.res}begin
Application.Initialize;
Application.CreateForm(TFormSplash, FormSplash);
FormSplash.ShowModal;
FormLogin:=TFormLogin.Create(application); //登录窗口
//ok:=false;
FormLogin.ShowModal;
if ok=true then
begin
Application.CreateForm(TFormMain, FormMain);
Application.Run;
end else
//FormLogin.ModalResult:=mrcancel;
end.
登录窗体代码
unit Unit2;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, DBTables, StdCtrls,ExtCtrls;type
TFormLogin = class(TForm)
Edit1: TEdit;
ComboBox1: TComboBox;
Button1: TButton;
DataSource1: TDataSource;
Query1: TQuery;
Database1: TDatabase;
Label1: TLabel;
Label2: TLabel;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations } public
{ Public declarations } end;var
FormLogin: TFormLogin;
UserID:string;
count:integer; //登录次数
ok:boolean;
implementationuses Unit_FormMain;{$R *.dfm}procedure TFormLogin.Button1Click(Sender: TObject);
begin
count:=0;
ok:=false;
while ok do
begin
//判断用户输入的工号是否为空
if FormLogin.ComboBox1.Text='' then
begin
ShowMessage('工号不能为空!');
Exit;
end;
if FormLogin.Edit1.Text='' then
begin
ShowMessage('密码不能为空');
Edit1.SetFocus;
Exit;
end;
if (FormLogin.ComboBox1.Text<>'')and (FormLogin.Edit1.Text<>'') then
begin
while count<3 do
begin
FormLogin.Query1.close;
FormLogin.Query1.sql.clear;
FormLogin.Query1.sql.add('select * from OperInfo where ID='''+Formlogin.ComboBox1.Text+'''');
FormLogin.Query1.open;
if FormLogin.Query1.recordcount=0 then
begin
inc(count);
if count>=3 then
begin
messagedlg('您已经登录失败超过3次,程序将终止',mtWarning,[mbok],0);
close;
application.Terminate;
end else
begin
Showmessage('工号或密码不正确!');
Exit;
//self.ModalResult:=mrcancel;
//FormLogin.Query1.close;
Edit1.Text:='';
Edit1.SetFocus
end;
end;
if FormLogin.Query1.recordcount>0 then
begin
ok:=true;
end; end;
end;
end;
end;procedure TFormLogin.Button2Click(Sender: TObject);
begin
application.Terminate;
end;procedure TFormLogin.FormCreate(Sender: TObject);
begin
//判断Query1中的当前记录是否是最后一个记录
while not Query1.Eof do
begin
//将当前纪录值添加到ComboBox1组合框中
ComboBox1.AddItem(Query1.FieldValues['ID'],sender);
//将当前记录下移一个
Query1.MoveBy(1);
end;
end;end.
主窗体dpr代码
program Project_FormMain;uses
Forms,
Controls,
Unit_FormMain in 'Unit_FormMain.pas' {FormMain},
Unit1 in 'Unit1.pas' {FormSplash},
Unit2 in 'Unit2.pas' {FormLogin};{$R *.res}begin
Application.Initialize;
Application.CreateForm(TFormSplash, FormSplash);
FormSplash.ShowModal;
FormLogin:=TFormLogin.Create(application); //登录窗口
//ok:=false;
FormLogin.ShowModal;
if ok=true then
begin
Application.CreateForm(TFormMain, FormMain);
Application.Run;
end else
//FormLogin.ModalResult:=mrcancel;
end.
------------------------------------------
program Project1;uses
SysUtils,
Forms,
Unit1 in 'Unit1.pas' {FormSplash},
Unit2 in 'Unit2.pas' {FormLogin},
Unit_FormMain in 'Unit_FormMain.pas' {FormMain};{$R *.res}begin
{
Application.Initialize;
Application.CreateForm(TFormSplash, FormSplash);
Application.CreateForm(TFormLogin, FormLogin);
Application.Run;
} Application.Initialize;
FormSplash := TFormSplash.Create(application); ////创建
FormSplash.Show; ////显示
FormSplash.Update; ////更新
SysUtils.sleep(2000); //显示2秒
//
FormLogin := TFormLogin.Create(application); //登录窗口
//
FormSplash.Hide; ////隐藏
freeAndNil(FormSplash); ///释放
//
FormLogin.ShowModal; //ok:=false; if unit2.ok then
begin
freeAndNil(FormLogin);
Application.CreateForm(TFormMain, FormMain);
Application.Run;
end
else
//FormLogin.ModalResult:=mrcancel;end.-------------------------------------------------------
procedure TFormLogin.Button1Click(Sender: TObject);
中代码比较乱,ok:= false,永远不会进入while循环!
而且上面2个if语句已经判断不为空,
(FormLogin.ComboBox1.Text <>'')and (FormLogin.Edit1.Text <>'') 多余。
while count <3 do 也不对,进去就出不来,
直到 inc(count) 三次,退出。
按照上面意思,应该点击3次button1,做3次登录动作,都失败退出程序。unit Unit2;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DB, DBTables;type
TFormLogin = class(TForm)
Edit1: TEdit;
ComboBox1: TComboBox;
Button1: TButton;
Button2: TButton;
Query1: TQuery;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations } end;var
FormLogin: TFormLogin; ////全局变量,定义时付初值,这3个变量最好放入public中,这里也可
userId: string;
count: integer = 0;
ok: boolean = false;implementation{$R *.dfm}procedure TFormLogin.Button1Click(Sender: TObject);
begin //判断用户输入的工号是否为空
if self.ComboBox1.Text = '' then
begin
ShowMessage('工号不能为空!');
Exit;
end;
if self.Edit1.Text = '' then
begin
ShowMessage('密码不能为空');
Edit1.SetFocus;
Exit;
end; if count < 3 then
begin
self.Query1.close;
self.Query1.sql.clear;
self.Query1.sql.add('select * from OperInfo where ID=''' +
Formlogin.ComboBox1.Text + '''');
self.Query1.open;
if not self.Query1.IsEmpty then
begin
ok := true;
self.Close;
end
else
begin
inc(count);
Showmessage('工号或密码不正确!');
//Exit;
//self.ModalResult:=mrcancel;
//FormLogin.Query1.close;
Edit1.Text := '';
Edit1.SetFocus
end; end
else
begin
messagedlg('您已经登录失败超过3次,程序将终止', mtWarning, [mbok],
0);
//close;
application.Terminate;
end;end;procedure TFormLogin.Button2Click(Sender: TObject);
begin
application.Terminate;
end;procedure TFormLogin.FormCreate(Sender: TObject);
begin
//判断Query1中的当前记录是否是最后一个记录
ComboBox1.Clear; ////如果不是累计附加,先清空是好习惯!
while not Query1.Eof do
begin
//将当前纪录值添加到ComboBox1组合框中
ComboBox1.Items.Add(Query1.fieldByName('ID').AsString);
////fieldByName().AsString 速度快
//将当前记录下移一个
Query1.MoveBy(1);
end;
end;end.
ComboBox1.Items.Clear;如果好使别忘给分 :)
如果count到了3的时候退出之前应该重新初始化count.
.......
else
begin
count := 0;
messagedlg('您已经登录失败超过3次,程序将终止', mtWarning, [mbok],
0);
//close;
application.Terminate;
end;
.....
Application.Initialize;
Application.CreateForm(TFormSplash, FormSplash);
FormSplash.ShowModal;
FormLogin:=TFormLogin.Create(application);
//ok:=false;
FormLogin.ShowModal; //注意这里
if ok=true then
begin
Application.CreateForm(TFormMain, FormMain);
Application.Run;
end else
//FormLogin.ModalResult:=mrcancel;
end.ShowModal并不会使程序停止继续执行
登录窗体弹出来后 程序会立即判断ok是不是true 而这时窗体刚刚创建 还没有输入任何信息 ok肯定是false的建议你直接在FormLogin当中验证登录信息 而不要在这个文件中判断
不应该用模式,改为非模式,再sleep就可以了.
unit UFrmLogin;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DB, ADODB;type
TFrmLogin = class(TForm)
Button1: TButton;
Button2: TButton;
Edit1: TEdit;
QUERY1: TADOQuery;
Edit2: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
FrmLogin: TFrmLogin;
UserID:string;
count:integer; //登录次数
ok:boolean;implementation{$R *.dfm}procedure TFrmLogin.Button1Click(Sender: TObject);
var
blogin: boolean;
begin
count:=0;
blogin:=false;
while not blogin do
begin
//判断用户输入的工号是否为空
if Edit1.Text='' then
begin
ShowMessage('工号不能为空!');
Exit;
end;
if Edit2.Text='' then
begin
ShowMessage('密码不能为空');
Edit1.SetFocus;
Exit;
end;
if (Edit1.Text <>'')and (Edit2.Text <>'') then
begin
while count <3 do
begin
if (trim(edit1.Text)<>'8888') and (trim(edit2.text)<>'8888') then
begin
inc(count);
if count>=3 then
begin
messagedlg('您已经登录失败超过3次,程序将终止',mtWarning,[mbok],0);
blogin := true;
Application.Terminate;
end else
begin
Showmessage('工号或密码不正确!');
break;
Edit1.Text:='';
Edit1.SetFocus;
end;
end else
begin
ok:=true;
blogin := true;
FrmLogin.Close;
Break;
end;
end;
end;
end;
end;end.
--这部分代码 project->view Source 中
program PLogin;uses
Forms,
UFrmMain in 'UFrmMain.pas' {FrmMain},
UFrmFlash in 'UFrmFlash.pas' {FrmFlash},
UFrmLogin in 'UFrmLogin.pas' {FrmLogin};{$R *.res}begin
Application.Initialize;
Application.Title := '登录测试'; Application.CreateForm(TFrmflash, Frmflash);
Frmflash.ShowModal;
FrmFlash.free;
FrmFlash := Nil;
ok := false; if not Assigned(FrmLogin) then
Application.CreateForm(TFrmLogin, FrmLogin);; //登录窗口
FrmLogin.ShowModal;
FrmLogin.free;
FrmLogin := Nil; if ok=true then
begin
Application.CreateForm(TFrmMain, FrmMain);
Application.Run;
end else
//FormLogin.ModalResult:=mrcancel;
end.
application.Terminate;
整个程序退出了,再次运行程序,会初始化count:=0;
To 阿日:
我照你的FormMain的dpr写语句,但出现了Access voilation at Adress...
请问怎么解决??上面集中其他方法出现登录界面时还是一闪就过了
FormSplash的2秒程序我已经用Timer设置号了,FormMain的dpr运行还是有问题
我测试的FormMain 是空窗体,看看你FormMain.onCreate里面有没有代码,贴上来。
onCreate, onShow, onActivate, onPaint 里面的代码
onClose中是否有caFreee,
如果这样,
下面的代码会出访问错误问题。Application.CreateForm(TFrmflash, Frmflash);
Frmflash.ShowModal;
FrmFlash.free;
FrmFlash := Nil;
ok := false;一半快闪窗体都在主窗体中控制,最好不要在快闪窗体自己定时、退出。
快闪窗体只显示信息,不要有执行代码。
unit Unit_FormMain;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Menus, DB, ADODB,ExtCtrls;type
TFormMain = class(TForm)
MainMenu1: TMainMenu;
MenuDataEnter: TMenuItem;
N1: TMenuItem;
N2: TMenuItem;
MenuQuery: TMenuItem;
CommonQuery: TMenuItem;
HighQuery: TMenuItem;
MenuDatabase: TMenuItem;
N3: TMenuItem;
N4: TMenuItem;
MenuUser: TMenuItem;
InfoManage: TMenuItem;
ClassManage: TMenuItem;
InfoModify: TMenuItem;
N5: TMenuItem;
Conn: TADOConnection;
ADOQuery1: TADOQuery;
ADOQuery2: TADOQuery;
procedure FormShow(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
//声明窗体级函数WriteName用于将数据库备份文件写入文件名为bfwj.txt的文本文件
Procedure WriteName(Name:String);
end;var
FormMain: TFormMain;
UserID:String;//记录工号
UserPwd:String;//记录用户密码
UserCLass:String;//记录用户权限
AdoConn:String;//记录数据库连接字符
path:String;//记录数据库备份文件存放的路径implementationuses Unit1, Unit2;{$R *.dfm}procedure TFormMain.FormShow(Sender: TObject);
begin
if UserClass='0' then
begin
FormMain.MenuDataEnter.Enabled:=true;
FormMain.MenuQuery.enabled:=true;
FormMain.MenuDatabase.Enabled:=true;
FormMain.MenuUser.Enabled:=true;
end;
if UserClass='1' then
begin
FormMain.MenuDataEnter.Enabled:=true;
FormMain.MenuQuery.Enabled:=true;
FormMain.CommonQuery.Enabled:=true;
FormMain.HighQuery.Enabled:=false;
FormMain.MenuDatabase.Enabled:=false;
FormMain.MenuUser.Enabled:=false;
end;
if UserClass='2' then
begin
FormMain.MenuDataEnter.Enabled:=false;
FormMain.MenuQuery.Enabled:=true;
FormMain.CommonQuery.Enabled:=true;
FormMain.HighQuery.Enabled:=false;
FormMain.MenuDatabase.Enabled:=false;
FormMain.MenuUser.Enabled:=false;
end;
if UserClass='3' then
begin
FormMain.MenuDataEnter.Enabled:=false;
FormMain.MenuQuery.enabled:=true;
FormMain.CommonQuery.Enabled:=false;
FormMain.HighQuery.enabled:=true;
FormMain.MenuDatabase.Enabled:=false;
FormMain.MenuUser.enabled:=false;
end;
Exit;
end;end.FormSplash代码
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls;type
TFormSplash = class(TForm)
Timer1: TTimer;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
procedure Timer1Timer(Sender: TObject);
procedure Label4Click(Sender: TObject);
procedure Label3Click(Sender: TObject);
procedure Label2Click(Sender: TObject);
procedure Label1Click(Sender: TObject);
procedure FormClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
FormSplash: TFormSplash;implementation
uses unit2;
{$R *.dfm}procedure TFormSplash.Timer1Timer(Sender: TObject);
begin
FormSplash.Close;
end;procedure TFormSplash.Label4Click(Sender: TObject);
begin
FormSplash.Close;
end;procedure TFormSplash.Label3Click(Sender: TObject);
begin
FormSplash.Close;
end;procedure TFormSplash.Label2Click(Sender: TObject);
begin
FormSplash.Close;
end;procedure TFormSplash.Label1Click(Sender: TObject);
begin
FormSplash.Close;
end;procedure TFormSplash.FormClick(Sender: TObject);
begin
FormSplash.Close;
end;end.
Windows,
Messages,
Dialogs,.............;var
hMutex:HWND;
begin
hMutex:=CreateMutex(nil,false,'soft');
if GetLastError<>ERROR_ALREADY_EXISTS then
begin
Application.Title:='xxxx毕业设计;
Application.Initialize;
{try
Frm_splash:=TFrm_splash.Create(nil);
Frm_splash.Show;
Frm_splash.Update;
sleep(10000); }
Application.CreateForm(Tdm_main, dm_main);
Application.CreateForm(TFrm_main, Frm_main);
Application.CreateForm(TFrm_login, Frm_login);
finally
Frm_splash.Free;
end;
Frm_login.ShowModal;
frm_main.Show;
Application.Run;
end
else
application.MessageBox('程序已运行!','警告',mb_ok+mb_iconwarning);
ReleaseMutex(hMutex);