How close/open forms in Windows app?

R

Ronald S. Cook

I know how to close and open a form...

Well.. but can you tell me how can I do this.....

Example...

There are 2 forms...named formA and formB and each of them have their own
button named btnA and btnB

The application starts with formA

can you show me the code that... when I click on btnA in formA, It will
CLOSE formA and open formB...

Then when I click btnB from formB, it will close formB and open back formA

How can I do this? I don't want their visible set to .false

I want them really close and use no resources from my computer.. How?

Thanks for your help....
 
S

Siva M

In btnA's click event:

FormB b = new FormB();
b.Show();
this.Close();

Similarly in btnB's click event:

FormA a = new FormaA();
a.Show();
this.Close();

I know how to close and open a form...

Well.. but can you tell me how can I do this.....

Example...

There are 2 forms...named formA and formB and each of them have their own
button named btnA and btnB

The application starts with formA

can you show me the code that... when I click on btnA in formA, It will
CLOSE formA and open formB...

Then when I click btnB from formB, it will close formB and open back formA

How can I do this? I don't want their visible set to .false

I want them really close and use no resources from my computer.. How?

Thanks for your help....
 
P

Peter Duniho

Ronald S. Cook said:
There are 2 forms...named formA and formB and each of them have their own
button named btnA and btnB

The application starts with formA

can you show me the code that... when I click on btnA in formA, It will
CLOSE formA and open formB...

Then when I click btnB from formB, it will close formB and open back formA

There may be other ways, but the most straightforward way I know of is to
use the Application.Run() overload that has no parameters, and then show
each form explicitly. You'll have to edit the Program class's Main() method
to do this, opening the form first desired ("formA" in your example)...each
button will close its owning form and show the other one.

You'll also have to add something to explicitly exit the
application...probably handle that in the close event of each form, in which
the Application.Exit() or Application.ExitThread() method is called unless
the form was explicitly closed by the button that opens the other form.

Here is some sample code (minus all the default stuff...hopefully it's
reasonably clear where you would have to add this):

In the Program.Main() method (replaces the default statement,
"Application.Run(new Form1())"):

(new Form1()).Show();
Application.Run();
In each of the forms you want, something like this (in addition to
everything else...and of course, the two new methods need to be hooked up in
the designer to the correct events):

public partial class Form1 : Form
{
private bool _fExitAppOnClose = true;

private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();

_fExitAppOnClose = false;
form2.Show();
Close();
}

private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
if (_fExitAppOnClose)
{
Application.Exit();
}
}
}
 
C

Cor Ligthert [MVP]

Ronald,

Have a look at the

Environment.Close(0); // the 0 means normal exit

Cor
 

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