I'd like to restart my main form on clicking a button. For instance:
In program.cs:
Application.Run(new MainForm(data))
In MainForm.cs:
private void btn1_Click(object sender, EventArgs e)
{
MainForm newForm = new MainForm(newData);
this.close();
Application.Run(newForm);
}
So that my new Main window is the new instance of MainForm. How can I do that in a way such that the first instance is cleared from memory?
EDIT
I.e. I'd like my first instance of MainForm to completely disappear - is that the same as just calling this.Hide()? Or calling this.Close() and then setting it up so that the application exits only when the last window is closed, and not when the main window is closed?
No. Starting a second message loop on a single thread is not a valid operation. You can use Form.ShowDialog instead:
Hide();
MainForm newForm = new MainForm(newData);
newForm.Closed += (s, args) => Close();
newForm.ShowDialog();
Or if you'd like to close the old instance:
Hide();
MainForm newForm = new MainForm(newData);
newForm.ShowDialog();
Close();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With