我做了个登录界面上面有两个button 两个edit 一个SQLQuery1控件  一个SQLConnection1控件(连接正常)代码如下
procedure TForm4.entryClick(Sender: TObject);
var
str,str1: string;
begin
str1:=trim(edit1.Text);
str:=trim(edit2.Text);
SQLQuery1.Locate('name', str1, []);
if trim(SQLQuery1.fieldbyname('name').AsString )=str1 then
begin
if trim(SQLQuery1.fieldbyname('password').AsString )=str then
begin
ModalResult := 1;
success := True;
end
else
begin
MessageDlg('对不起,密码错误,你无权进入系统!', mtError, [mbOK], 0);
ModalResult := 0;
end;
end;
end;工程代码如下:begin
Application.Initialize;
Application.CreateForm(Tform4, form4); //登录窗口
if form4.ShowModal = 1  then
begin
  Application.Initialize;
  Application.CreateForm(TForm1, Form1); //主窗口运行
  Application.CreateForm(TForm3, Form3);
  Application.CreateForm(TDataModule1, DataModule1);
  Application.CreateForm(TForm5, Form5);
  Application.CreateForm(TForm7, Form7);
   end;
  form4.Free;
  application.Terminate ;
  Application.Run ;
  
end.
但是输入正确的用户名和密码后 点击确定 系统自动结束了 无法进入到主界面  请问代码错在哪里了?

解决方案 »

  1.   

    Application.CreateForm(Tform4, form4); //登录窗口你这样创建窗体,编译器会认为form4为主窗体,你把form4.free掉当然程序结束了你可以这样创建:
    form4:=TForm4.Create(application);
    try
       if form4.ShowModal=1 then
       //...;
    finally
        form4.free;
    end;
      

  2.   

    你可以自己写个小程序试验一下,
      Application.Initialize;
      Application.CreateForm(TLOGIN, LOGIN);
      Application.Run;执行的时候跟踪一下会发现,Application.Run运行之后LOGIN窗体才出现。
     你代码的这种写法我没发理解。可不可以用其他的方法,在确定按钮里写代码让主窗体运行。
      

  3.   

    问题不光是出在form4.free 那,即使没有这句,也不会像预期的那样正常运行。