I have a MdiParent with two buttons, when button1 is click form1 will show and when button2 is click form2 will show then form1 will hide and vice versa. For example I click the button1 the form1 opens, then I type in the textBox of form1. After that, I click button2 then the form2 shows and form1 hides. The problem is when I click again the button1 the form1 shows but the previously type in the textBox is missing. It seems that forms are not hiding but closing instead then open again when a button is clicked.
public static class Extensions
{
public static void openForm<T>(this T frm, Form parent) where T : Form, new()
{
foreach (Form childForm in parent.MdiChildren)
{
childForm.Visible = false;
}
foreach (Form form in Application.OpenForms)
{
if (form.GetType() == typeof(T))
{
form.Visible = true;
return;
}
}
frm = new T();
frm.MdiParent = parent;
frm.WindowState = FormWindowState.Normal;
frm.StartPosition = FormStartPosition.CenterScreen;
frm.MaximizeBox = false;
frm.MinimizeBox = false;
frm.Show();
}
}
This is how I open the childform:
EDIT
public partial class MainForm : Form
{
form1 newform1 = new form1();
form2 newform2 = new form2();
private void Button1_Click(object sender, EventArgs e)
{
newform1.openForm(this);
}
private void Button2_Click(object sender, EventArgs e)
{
newform2.openForm(this);
}
}