"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();
}
}
}