469,953 Members | 1,959 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,953 developers. It's quick & easy.

How close/open forms in Windows app?

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....
Oct 8 '06 #1
3 10473
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();

"Ronald S. Cook" <rc***@westinis.comwrote in message
news:ef**************@TK2MSFTNGP04.phx.gbl...
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....
Oct 8 '06 #2
"Ronald S. Cook" <rc***@westinis.comwrote in message
news:ef**************@TK2MSFTNGP04.phx.gbl...
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();
}
}
}


Oct 8 '06 #3
Ronald,

Have a look at the

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

Cor

"Ronald S. Cook" <rc***@westinis.comschreef in bericht
news:ef**************@TK2MSFTNGP04.phx.gbl...
>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....

Oct 8 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by jrefactors | last post: by
reply views Thread by markusp1982 | last post: by
2 posts views Thread by Paul Steele | last post: by
3 posts views Thread by Isabel | last post: by
1 post views Thread by Bryan | last post: by
3 posts views Thread by rdemyan via AccessMonster.com | last post: by
6 posts views Thread by Ryan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.