By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,244 Members | 1,772 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,244 IT Pros & Developers. It's quick & easy.

How close/open forms in Windows app?

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
"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

P: n/a
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.