Question

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);
    }
 }
Was it helpful?

Solution

MDI does not in fact support hiding child windows. Winforms has a workaround for that, it will destroy the native window when you set its Visible property to false (or call Hide()) and recreate the window when you make it visible again.

Unfortunately there's a bug that bytes in your case. Destroying the native window will also make it disappear from the Application.OpenForms() collection. You depend on that collection to find the window back, it is not going to work and you'll always create a new instance of the form. Which of course doesn't have any text in that TextBox. Easy to see with the debugger btw.

So you cannot use the OpenForms collection. You can however use the MdiChildren collection to find it back. So rewrite it to:

    bool found = false;
    foreach (Form childForm in parent.MdiChildren) {
        if (form.GetType() = typeof(T)) form.Visible = found = true;
        else form.Visible = false;
    }
    if (!found) {
       // etc...
    }

OTHER TIPS

new form1().openForm(this);
new form2().openForm(this);

you are creating new instances of the form, the forms shown are not the forms which are hidden that's why the text in the textbox is missing. Try not to create new instance.

var someForm1 = new form1();
var someForm2 = new form2();

someForm1.openForm(this);

for form 2 obviously

someForm2.openForm(this);

try using form.hide(); for hiding the form and form.show(); for showing the form again..

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top