Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mainform trying to show on closing the login form

Tags:

delphi

This is the *.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.

Main 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 set to Modal :mrOK. Login Form is not auto created but on the list of available forms.

The problem is this : If you close the Login Form (without clicking the button) for a split second the Main Form shows and then it closes (and of course, the application closes as well). It happens very fast. Seems like a flicker.

How can I annulate this attempt of my main Form to try and show itself when I close the Login Form ?

Also, setting :

Application.MainFormOnTaskbar := False;

does not help either ...

like image 797
user3351050 Avatar asked Oct 29 '16 11:10

user3351050


2 Answers

Instead of hiding Main Form before you show Login you should use initialize Application to start with Main Form hidden. To do that just add Application.ShowMainForm := false before you create main form.

Application.Run that executes after you close your login form will show or not show Main Form based on value of Application.ShowMainForm variable that is by default true. So that will introduce flicker because main form will be shown before application terminates.

begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Application.ShowMainForm := false;
  Application.CreateForm(TForm1, Form1);
  Login;
  Application.Run;
end.
like image 81
Dalija Prasnikar Avatar answered Nov 19 '22 19:11

Dalija Prasnikar


Turn your Login procedure into function 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 don't see, what you are checking in you login form. I think you should just return TRUE (login passed) or FALSE as the result, instead of trying to manipulate with other forms.

like image 3
user6150786 Avatar answered Nov 19 '22 18:11

user6150786