郁闷一天了,程序明明没问题,为什么一关闭就退出?(50分)

  • 主题发起人 主题发起人 nofuture
  • 开始时间 开始时间
N

nofuture

Unregistered / Unconfirmed
GUEST, unregistred user!
unit Flogin;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, Mask;
type
TF_login = class(TForm)
Label1: TLabel;
Label2: TLabel;
BitBtn1: TBitBtn;
BitBtn2: TBitBtn;
Edit1: TEdit;
Combo1: TComboBox;
Label3: TLabel;
Label4: TLabel;
procedure BitBtn2Click(Sender: TObject);
procedure plogin;
procedure BitBtn1Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;
var
m:boolean = false;
s:integer = 0;
F_login: TF_login;
implementation
uses Main,Fdata;
{$R *.dfm}
procedure TF_login.BitBtn2Click(Sender: TObject);
begin
if m=false then
application.Terminate
else
close;
end;
procedure TF_login.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if m = false then
application.Terminate ;
end;
procedure TF_login.BitBtn1Click(Sender: TObject);
begin
s := s+1;
if combo1.Text = ''then
application.MessageBox('请输入用户名','提示',1)
else
if edit1.Text='' then
application.MessageBox('请输入密码','提示',1)
else
plogin;
end;
procedure TF_login.plogin;
begin
with F_data.user do
begin
close;
SQL.Clear ;
SQL.Add('select * from 员工表 where name=:name and password =:password');
parameters.ParamByName('name').Value := trim(combo1.Text );
parameters.ParamByName('password').Value := trim(edit1.Text );
open;
end;
if F_data.user.recordcount > 0 then
begin
m:=true;
close;
end
else
if s<3 then
begin
Application.MessageBox('用户名或密码不正确,请重新输入','提示', 1);
combo1.Clear;
edit1.Clear;
combo1.SetFocus ;
end;
end;
end.
郁闷一天了,为什么一关闭就退出呢?
弹出"用户名或密码不正确,请重新输入"后,点击确定后程序就Terminate了!我很奇怪,程序明明没有任何问题,为什么会这样呢?难道在其他地方还有设置?
 
我都试过了,似乎没有问题啊
 
程序不能正常运行,一般来说就是你的代码有问题.
 
看看,是不是我调用问题~!
procedure TMainFrm.FormShow(Sender: TObject);
begin
application.CreateForm(TF_login,F_login);
F_login.ShowModal ;
F_login.Free;
end;
 
var
m:boolean = false;//它默认就是Flase

procedure TF_login.BitBtn2Click(Sender: TObject);
begin
if m=false then//在你的代码不修改m变量之前,只要进入这里就必定执行Application.Terminate
application.Terminate
else
close;
end;
 
procedure TF_login.BitBtn2Click(Sender: TObject);
begin
if m=false then
application.Terminate
else
close;//这里执行完立刻会执行下面这个事件
end;
procedure TF_login.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if m = false then//没有修改过m,必定执行下面Application.Terminate;
application.Terminate ;
end;

兄弟,仔细审查一下你的逻辑!!!
 
是因为你的F_login是ShowModal出来的,然后你的BitBtn1,和BitBtn2的ModalResult都不是mrNone,导致必定关窗体,而这时 m:boolean = false;,所以程序终止
 
如果你用的是MDI窗體的話,你在Project-options選項里,把主窗體拖到左邊(即auto_create forms),
 
zqw0117
问题不在这个上面,应该是cactus123456说的,因为我BitBtn2Click本身就是要关闭的,这个是取消按扭,没问题,要得就是TERMINATE,关键是我点BitBtn1后的程序,m已经是TRUE了~!
我现在的问题是这个showmodal,还不太回用~!
不太理解这句话含义,我去查查帮助:
"ModalResult都不是mrNone,导致必定关窗体"
 
看了看showmodal的帮助:
If the form contains buttons with a ModalResult property set to a value other than mrNone, the form automatically closes when the user clicks one of those buttons and returns the ModalResult value as the return value of ShowModal.
看来确实是这个问题
 
晕~!没人能帮忙解决吗?
 
cactus123456说的很明确了.你的那个button1是dlg的吧,你直接在它的click写的事件.
应该在时间里面先加上button1.modalresult=mrnone;
当你检验密码通过了,在设置button1.modalresult=mrok;
否则的话,默认就是mrok,自然就把窗体关闭了,无论你的密码验证对否.

你自己用watch看一下就知道了.最好把你的那个plogin改成函数,通过返回值判断是否通过,然后设置按钮的modalresult.
 
原来如此简单,哎~!
只需要把两个按扭的modalresult设为mrnone就搞定~!
 
答案已经接受了,多谢cactus123456和hityou
俺配了20和30分,抱歉哦,只有这么多了~!
 
后退
顶部