Mainform tries to show when closing login form

This is * .dpr:

program Project1; uses Vcl.Forms, Unit1 in 'Unit1.pas' {Form1}, Unit2 in 'Unit2.pas' {Form2}; {$R *.res} var MainForm: TForm1; begin Application.Initialize; Application.MainFormOnTaskbar := True; Application.CreateForm(TForm1, Form1); Login; Application.Run; end. 

Login form:

 unit Unit2; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls; type TForm2 = class(TForm) Button1: TButton; private { Private declarations } public { Public declarations } end; var Form2: TForm2; procedure Login; implementation {$R *.dfm} Uses Unit1; procedure Login; begin with TForm2.Create(nil) do try Application.MainForm.Hide; if ShowModal = mrOK then Application.MainForm.Show else Application.Terminate; finally Free; end; end; end. 

Basic form:

 unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls; type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} Uses Unit2; procedure TForm1.Button1Click(Sender: TObject); begin Login; end; end. 

Both buttons are installed in Modal: mrOK. The login form is not automatically created, but in the list of available forms.

The problem is this: If you close the login form (without pressing a button) within the second second, the Main form is displayed, and then it closes (and, of course, the application closes). It happens very fast. It seems to flicker.

How can I cancel this attempt of my main form in order to try to show it when I close the login form?

In addition, the installation:

 Application.MainFormOnTaskbar := False; 

Does not help...

+7
delphi
source share
2 answers

Instead of hiding the main form before showing the login, you should use initialize Application to start with a hidden main form. To do this, simply add Application.ShowMainForm := false before creating the main form.

Application.Run , which is executed after you close your login form, will display or not display the main form based on the value of the Application.ShowMainForm variable, which is true by default. Thus, this will cause flickering because the main form will be shown before the application is completed.

 begin Application.Initialize; Application.MainFormOnTaskbar := True; Application.ShowMainForm := false; Application.CreateForm(TForm1, Form1); Login; Application.Run; end. 
+5
source share

Turn your login procedure and change the main code:

  program Project1; uses Vcl.Forms, Unit1 in 'Unit1.pas' {Form1}, Unit2 in 'Unit2.pas' {Form2}; {$R *.res} var MainForm: TForm1; begin Application.Initialize; Application.MainFormOnTaskbar := True; if Login then begin Application.CreateForm(TForm1, Form1); Application.Run; end else Application.Terminate; end. 

I do not see what you are checking in the login form. I think you should just return TRUE (the login has passed) or FALSE as the result, instead of trying to manipulate other forms.

+3
source share

All Articles