Jump to content
robertjohns

Form Creation

Recommended Posts

I am creating a Form1 and freeing it after some time

 

Now I am trying to check if Form1 is created then create Form2

 

If Assigned(Form1) then

Application.CreateForm(TForm2, Form2);

 

But It does not seems to work any suggestion

Share this post


Link to post

Hi...:classic_cool:

How about source code? Complete project?

What is the mainform? Don´t free the mainform! :classic_huh:

 

Edited by haentschman

Share this post


Link to post

No I am not freeing MainForm

 

Form2 is MainForm .. I am trying to check If Form1 is created then create Form2 which is Mainform

Share this post


Link to post
1 hour ago, robertjohns said:

If Assigned(Form1) then

Application.CreateForm(TForm2, Form2);

6 minutes ago, robertjohns said:

Form2 is MainForm .. I am trying to check If Form1 is created then create Form2 which is Mainform

 

Makes not much sense right now.

Share this post


Link to post

The first form created is always the main one. We don't know how you create Form1.

Share this post


Link to post
50 minutes ago, Stano said:

The first form created is always the main one. We don't know how you create Form1.

I have just give an Example .. I know I can't free MainForm .. I have simply given the name here Form1 and Form2

Share this post


Link to post
17 minutes ago, Uwe Raabe said:

Looks like a splash or login form.

Yes Exactly I create a Splash Form and free it then check If Splash is created before creating login form

Edited by robertjohns

Share this post


Link to post

Splash should probably shown modal. Then when it closes, open the other form(s).

Share this post


Link to post

A modal Splash form would probably block the startup sequence.

Edited by Uwe Raabe

Share this post


Link to post
1 hour ago, robertjohns said:

I have just give an Example .. I know I can't free MainForm .. I have simply given the name here Form1 and Form2

I search, I search and I don't see it anywhere :classic_wink: 
Put your minimum code here. So everyone here is just guessing.

Share this post


Link to post

"Does not seem to work" how do you deduce this?

Do you see error messages, get unexpected behavior? If so, what are the errors, what behavior do you expect, and what is happening instead of that?

Share this post


Link to post
9 hours ago, robertjohns said:

I am creating a Form1 and freeing it after some time

 

Now I am trying to check if Form1 is created then create Form2

 

If Assigned(Form1) then

Application.CreateForm(TForm2, Form2);

 

But It does not seems to work any suggestion

Depends on where you do this check and where you free the form1. If you let the IDE autocreate the forms you add to the project then the first form you add will become the main form and closing it will terminate the application. If you need to show some form before the main form is created you have to edit the project DPR file. The typical scenario is a login form shown before the main form is even created. The DPR file then has to look like this:

 

uses
  ... list of data module and form units
{$R *.res}

begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  ... Application.CreateForm calls for datamodules the login
      form may need
  if TLoginForm.Execute then 
  begin 
    Application.CreateForm(TMainForm, MainForm);
    ... more CreateForm statements for autocreated stuff
    Application.Run;
  end; 
end.

  

TLoginForm is not autocreated, the Execute method is a public class method that internally creates an instance of the form and shows it modally, then gets any required user input from the form and stores it elsewhere if needed (e.g. in a datamodule or settings store). The Execute method is a function returning a boolean that returns true if the login was successful and false if it failed. This way the main form is not even created if login failed.

Edited by PeterBelow

Share this post


Link to post
10 hours ago, robertjohns said:

But It does not seems to work any suggestion

if you only need to show a "form" if another one imposed a condition, like in login... try this

 

program Project1;

uses
  System.SysUtils {FreeAndNil()} ,
  System.UITypes {TModalResult} ,
  Vcl.Forms,
  uMainForm in 'uMainForm.pas' {Form1} ,
  uLoginForm in 'uLoginForm.pas' {Form2};

{$R *.res}

var
  LoginForm   : TForm2;
  LAllItsRight: TModalResult;

begin

  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Application.CreateForm(TForm1, Form1);
  //
  LAllItsRight := mrNo;
  LoginForm    := TForm2.Create(nil);
  try
    LAllItsRight := LoginForm.ShowModal;
  finally
    FreeAndNil(LoginForm); // to avoid a pointer-on-space
  end;
  //
  if (LAllItsRight = mrOk) then
    Application.Run;

end.

/// ----  form2 = LoginForm
var
  Form2: TForm2;

implementation

{$R *.dfm}

procedure TForm2.Button1Click(Sender: TObject);
begin
  Self.ModalResult := mrOK; // just set "modalresult before close it"
end;

end.
// here, simulating a intervale to wait before close LoginForm etc...

procedure TForm2.FormCreate(Sender: TObject);
begin
  // Timer1.Enable = true by default
  Timer1.Interval := 3000; // 3secs
end;

procedure TForm2.Timer1Timer(Sender: TObject);
begin
  Self.ModalResult := mrOK;  // you can to do any actions before set "modalresult" to close it!
end;

 

Edited by programmerdelphi2k

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×