So I have put both the forms in the Program.cs so I can close them without ending the application. But once I close both of them the application ends, even when I open the other one back up.
Program.cs code:
Application.Run(new Form1());
Application.Run(new Form2());
I have a button on Form1 that opens Form2 and closes Form1
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 Form2 = new Form2();
Form2.Show();
this.Close();
}
And then I have done the same on Form2
(open Form1
and close Form2
)
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 Form1 = new Form1();
Form1.Show();
this.Close();
}
If I press on the button on Form1
, Form2
will open, but once I try come back it closes the whole Application
. Now I know this.Hide()
works but I need the forms to close, not hide. I have a way to make it work: By adding a 3rd form into Program.cs and then hiding it upon loading, but I wondered if there was a way to do it without the 3rd form.