How to display a form?

  • Thread starter Thread starter Keith Smith
  • Start date Start date
K

Keith Smith

Sorry to ask about something so simple. I have researched this for the past
hour and just can't find what I am looking for.

I have a simple C# aplication that has two forms. They are called Form1 and
Form2.

The app starts in Form1. What code do I need to close Form1 and display
Form2?
 
..
..
..
Form2 f = new Form2;
f.Show();
this.Close();
..
..
..
if your Form1 is the one with the main() or you have a menu level form?
ask if need more.

MajorTom
 
That almost worked.

It loaded Form2 breifly (just long enough to display a MessageBox) and then
the whole applcation exited.

If I leave out the "this.Close();" then Form2 stays displayed, except so
does Form1. How can I make it so that Form2 stays open and Form1 closes?

Thank you!
 
Keith Smith said:
How can I make it so that Form2 stays open and
Form1 closes?

When your main form closes, the program ends. Try using Hide instead
of Close.

P.
 
When your main form closes, the program ends. Try using Hide instead of
Close.

P.

That works to a point... But when I close Form2 the application still
appears to stay running in the background (I concluded that because I have
to hit the "stop" button in order to make it completely exit).

Any ideas how to make the application completely end when I click on the "X"
to close Form2?
 
Your application started with this line:
Application.Run(new Form1());

this call waits until the form1 ends. Than the execution will continue from
the next line which will exit the function and ultimately exiting the entire
application because its inside the "Main" function. Now when you show the
Form2 and then you close it, you need to call the close method of form1
also. Do something like this:

//inside your Form1 at the time of showing the form2
this.Hide();
Form2 form2 = new Form2();
form2.ShowDialog( this );
//form1 execution is stuck here and waiting for form2 to close so it can
continue doing its thing.
Close(); //the application exits cuz form1 is closing. This line will be
called when the Form2 closes.

Hope that helps.

Ab.
http://joehacker.blogspot.com.
 
//inside your Form1 at the time of showing the form2
this.Hide();
Form2 form2 = new Form2();
form2.ShowDialog( this );
//form1 execution is stuck here and waiting for form2 to close so it can
continue doing its thing.
Close(); //the application exits cuz form1 is closing. This line will
be
called when the Form2 closes.

Oh I get it now! Thank you so much!
 
Keith said:
Sorry to ask about something so simple. I have researched this for the past
hour and just can't find what I am looking for.

I have a simple C# aplication that has two forms. They are called Form1 and
Form2.

The app starts in Form1. What code do I need to close Form1 and display
Form2?

I believe all you need is this.

//to close form1
form1.Close();

//open form2
form2.Show();


I think you also need to open form2 before you close form1.


-John
 
Well, sort of.

You'd need to create an instance of it first:

form2 frmOptions = new form2();
frmOptions.Show();
 
Back
Top