C# form close problem

J

Jason Huang

Hi,

In my C# windows form project, how do I in a formA open another formB, and
close formA when formB is opened?
Thanks for help.


Jason
 
M

Morten Wennevik

Hi Jason,

You are probably better off having formA and formB share a common parent
responsible for the message loop.
If formA is started from Main, have Main start both forms. Pass
properties to formB via Main.

What are you trying to do?
 
G

Guest

You can do so using Show and Close method of the Form. Let us suppose that
you have 3 forms, Form1 (the main form), Form2 and Form3.

From Form2, if you want to open Form3 and close Form2, you can use the
following code:

Form3 f3 = new Form3();
f3.Show(); // Opens the Form3

this.Close(); // Closes the current form (Form2)

If you close Form1 (which might be the main form), the entire application
will close.
 
M

Morten Wennevik

You can do so using Show and Close method of the Form. Let us suppose
that
you have 3 forms, Form1 (the main form), Form2 and Form3.

From Form2, if you want to open Form3 and close Form2, you can use the
following code:

Form3 f3 = new Form3();
f3.Show(); // Opens the Form3

this.Close(); // Closes the current form (Form2)

If you close Form1 (which might be the main form), the entire application
will close.

Not if you fire up another Form in your Main method

Application.Run(new Form1());
Application.Run(new Form2()); // will start when Form1 ends
 
G

Guest

Not sure you want to create two forms in one application that way, I'm pretty
sure you end up on two different threads.

You can also start the application without tying it to a particular form

Form1 frm1 = new Form1();
frm1.Show();
Application.Run();

Then you can open and or close forms without the problem of shutting down
the application when you close a form, or having to leave a form in memory
while your app is running even if you don't need it.

Of course it DOES introduce the problem of your having to specifically end
your application.

WhiteWizard
aka Gandalf
MCSD.NET, MCAD, MCT
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top