If you are not creating multiple copies of your forms, I find this to be the easiest method to use. Have the form itself create a handle to it with:
public partial class Form1
{
public static Form1 Current;
public Form1()
{
InitializeComponents();
Current = this;
}
}
public partial class Form2
{
public static Form2 Current;
public Form2()
{
InitializeComponents();
Current = this;
}
private buttonForm2_click(object sender,EventArgs e)
{
Form1.Current.Show();
}
}
It gets a little more complicated if people close the form. So in the Closing handler do (if you want to keep a handle):
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
Visible = false;
WindowState = FormWindowState.Minimized;
}
The e.Cancel
will keep the form from actually being destroyed, requiring you to create it again. Though if you want it destroyed, you could always create a new one again by converting your Current variable to a Current property that create's a new one in it's get;
.