Jump to content
jovenbarola

How to open form2 then close form1

Recommended Posts

Posted (edited)

Hello,

 

May I seek help on how to accomplish this, (I'm a beginner studying delphi)

 

My startup is form1

I want to Open form2, then closing current form (form1)?

 

Then I want to pass variable or some string parameter to form2?

 

Here is my current code, it show form2, but it doesnt close immediatly the form1

 

procedure TForm1.Button1Click(Sender: TObject);
begin
  Form2 := TForm2.Create(nil);
  try
    Form2.ShowModal;
  finally
    Form2.Free;
    Form1.Release;
  end;
end;

 

Thanks in advance

Edited by jovenbarola

Share this post


Link to post

Form2.ShowModal; stops further code execution. Put a breakpoint on that line and you'll see it.

Share this post


Link to post
Posted (edited)
2 hours ago, jovenbarola said:

Hello,

 

May I seek help on how to accomplish this, (I'm a beginner studying delphi)

 

My startup is form1

I want to Open form2, then closing current form (form1)?

 

Then I want to pass variable or some string parameter to form2?

 

Here is my current code, it show form2, but it doesnt close immediatly the form1

  


procedure TForm1.Button1Click(Sender: TObject);
begin
  Form2 := TForm2.Create(nil);
  try
    Form2.ShowModal;
  finally
    Form2.Free;
    Form1.Release;
  end;
end;

 

Thanks in advance

Remember, YOUR FIRST FORM should always be alive, don't close, free or release it. If you close your FIRST Form, the application also terminate.

 

In your example, when you'll exit from Form2 your application will close.  And it would have closed even if I had used the Show method instead of ShowModal.
If you need to "hide" the main Form use the Hide method. For other Forms (not MDI) you can also use the Close method ... as a default action the Form will be hidden (i.e. put in HIDE state).

 

https://docwiki.embarcadero.com/Libraries/Athens/en/Vcl.Forms.TForm

 

Bye

Edited by DelphiUdIT

Share this post


Link to post
7 hours ago, DelphiUdIT said:

Remember, YOUR FIRST FORM should always be alive, don't close, free or release it. If you close your FIRST Form, the application also terminate.

That is not quite accurate. Yes, the app will terminate if the MAIN Form is closed. But the FIRST Form does not need to be the MAIN Form. The MAIN Form is the first Form created with TApplication.CreateForm(). But you do not need to use CreateForm() to create and show a Form. It is possible to show a Form before the MAIN Form is established.

 

Share this post


Link to post
18 hours ago, Remy Lebeau said:

That is not quite accurate. Yes, the app will terminate if the MAIN Form is closed. But the FIRST Form does not need to be the MAIN Form. The MAIN Form is the first Form created with TApplication.CreateForm(). But you do not need to use CreateForm() to create and show a Form. It is possible to show a Form before the MAIN Form is established.

 

Yes, you are right, I expressed myself badly. I meant Main Form.

Since he wrote that he was a beginner I implied that Form1 was both the first Form and the main one (it is generally both the first Form and the main one when you create a new project with Delphi).

 

But of course is better to talk with the correct terms.

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

×