How to close a window without closing the whole program?

1.7k views Asked by At

in my application i have two forms let's say, LoginForm and AccountForm

The LoginForm is set as the main Form, it is the form when the user is able to login from to his account ( two TEdits and login Button ). When a user type his login details and connect, a new form which is AccountForm is opened.

How to close the LoginForm on login success without closing the whole application? or in such language how to use the code below to close the Login form only but not the application.

if (not IncludeForm.sqlquery1.IsEmpty) and (isblacklisted='0') and (isactivated='1')  then
begin // Login Successful *** Show the account window
AccountForm.Show;
LoginFrom.Close; // <----The problem is in this line, using this line causes the whole application to close***}
end;

Thankyou

2

There are 2 answers

1
menjaraz On BEST ANSWER

You can fetch here the source code of the excellent article Display a LogIn / Password Dialog Before the Main Form is Created by Zarko Gajic .

Excerpt:

program PasswordApp;

uses
  Forms,
  main in 'main.pas' {MainForm},
  login in 'login.pas' {LoginForm};

{$R *.res}

begin
  if TLoginForm.Execute then
  begin
    Application.Initialize;
    Application.CreateForm(TMainForm, MainForm) ;
    Application.Run;
  end
  else
  begin
    Application.MessageBox('You are not authorized to use the application. The password is "delphi".', 'Password Protected Delphi application') ;
  end;
end.
3
GolezTrol On

Don't make LoginForm the main form. If you create the loginform using LoginForm := TLoginForm.Create instead of Application.CreateForm, the form won't be set as the application main form. The first form created using Application.CreateForm will be the main form. You can edit your project file (.dpr) to alter it like this:

program YourApp;

uses
  Forms,
  fLoginForm in 'fLoginForm.pas' {LoginForm},
  fMainForm in 'fMainForm.pas' {MainForm};

{$R *.res}

begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  with TLoginForm.Create(nil) do
  try
    ShowModal;
  finally
    Free;
  end;
  Application.CreateForm(TMainForm, MainForm);
  Application.Run;
end.

You could also create your own application main loop that checks for specific forms being opened, but that's a bit harder and a bit more fragile than the solution above.