?
Solved

Create two forms but show one

Posted on 2005-04-23
12
Medium Priority
?
225 Views
Last Modified: 2010-04-05
In my project I have two forms: FOrm1 and Form2;

On Form1 create event I must to do some action to Form2. So I need to create first Form2 and after that Form1. But when I run my application wish to show just Form1 and Form2 to be show just when I ask.

I try to use:

  Application.CreateForm(TForm2, Form2);
  Application.CreateForm(TForm1, Form1);
  Application.Initialize;
  Application.Run;

but is show just form2.

  Application.CreateForm(TForm2, Form2);
  Application.CreateForm(TForm1, Form1);
  Form1.Show;
  Application.Initialize;
  Application.Run;

Show both;

  Application.CreateForm(TForm2, Form2);
  Form2.Hide;
  Application.CreateForm(TForm1, Form1);
  Form1.Show;
  Application.Initialize;
  Application.Run;

Still show both.

Help!
0
Comment
Question by:ginsonic
  • 4
  • 3
  • 2
  • +2
12 Comments
 
LVL 7

Expert Comment

by:DavidBirch2dotCom
ID: 13850603
Try
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Form1.Visible:= false;
  Application.CreateForm(TForm2, Form2);
  Application.Run;

David
0
 
LVL 7

Expert Comment

by:DavidBirch2dotCom
ID: 13850616
Opps sorry, you will also need to change your projects main form to the one you want to be shown, go:

Project > Options > Forms  and then change the main form to the one you want to be shown in this case form 2

David
0
 
LVL 15

Accepted Solution

by:
mikelittlewood earned 2000 total points
ID: 13850677
This seems to work fine

Application.Initialize;
Application.CreateForm(Tform2, Form2);
Application.ShowMainForm := False;
Application.CreateForm(TForm1, Form1);
Form1.Show;
Application.Run;
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 15

Expert Comment

by:mikelittlewood
ID: 13850689
Why do you need to create Form2 first but not show it?
0
 
LVL 7

Expert Comment

by:DavidBirch2dotCom
ID: 13850704
mike > "But when I run my application wish to show just Form1 and >>Form2 to be show just when I ask.<<" ;)
0
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 13850806
But that code does not show Form2 .. only Form1.
Then he can show Form2 when he wants.

Am I not understanding the question?
0
 
LVL 7

Expert Comment

by:DavidBirch2dotCom
ID: 13850855
I'm confused :(
0
 
LVL 34

Expert Comment

by:Slick812
ID: 13850883
hello ginsonic, I would Not create my Form 2 before my Form1, although you have found that you can not change the Form2 in the Form1 OnCreate Event, since the Form2 has not been created. . . . So I would add a procedre to the Form1 to be called in the OnCreate of Form2, so you can initialize the Form2 in a code block in form1. . . . . .

code for Form1 - -


  TForm1 = class(TForm)

  public
    { Public declarations }
    procedure SetForm2;

 - - - - - -

procedure TForm1.SetForm2;
begin
Form2.Width := Form2.Width+ 10;
Form2.Caption := 'Has been Changed';
end;


now code for Form2 OnCreate -

procedure TForm2.FormCreate(Sender: TObject);
begin
Form1.SetForm2;
end;

 - - - - - - - - - - - - - - - - - -

OR you could add public Variables (or a Record) to the Form1 which will be set in the OnCreate Event and then Read in the Form2 OnCreate event adn used to set whatever needs to be doen to Form2. . . .
Although there might be other things that may not allow you to do this sort of thing?
0
 
LVL 1

Expert Comment

by:jturpin
ID: 13851057

What I usually do is make Form2 a variable of Form1. Then you can create it when you need to.

So say maybe in TForm1.OnActivate:
  if (FirstLaunch) then
  begin
    Form2:= TForm2.Create(self);
    FirstLaunch:= false;
  end;


then in TForm1.OnClose say
  if Assigned(Form2) then
    Form2.Release;

Or if you only want to use Form2 in a given spot, say its a logon form then in TForm1.OnActivate say:
  Form2:= TForm2.Create(nil);
  try
    Form2.ShowModal; //etc
  finally
    Form2.Release;
  end; {try finally}

0
 
LVL 9

Author Comment

by:ginsonic
ID: 13852769
Sorry for delay in reply. I have a standart procedure named RefreshContent. This refresh dates from Form1 and Form2.
But I call this procedure on Form1 create moment, too. And on this time my Form2 isn't created yet so return an error message.

I thinking to a way. Remove Form2 from project and at Form1.Create to use Form2.Create; RefreshContent;

:) Look like jturpin think the same. Just I readed his comment ;)
0
 
LVL 34

Expert Comment

by:Slick812
ID: 13855204
maybe call the RefreshContent in your Form2 OnCreate?

procedure TForm2.FormCreate(Sender: TObject);
begin
Form1.RefreshContent;
end;

but you could just create the form2 in Form1 On Create, like you said. . .
0
 
LVL 9

Author Comment

by:ginsonic
ID: 13856140
Thanks to all for support!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Suggested Courses
Course of the Month15 days, 23 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question