Search code examples
c#mdichild

Why MDI child form not show when I insert textbox C#


I'm created Form1 and create MDI child form is Form2. Form2 window state is Maximized in Parent form.

My Some Code :

bool IsOpen = false;
foreach (Form f in Application.OpenForms)
{
    if (f.Text == "Manage Model")
    {
        IsOpen = true;
        f.Focus();
        break;
    }
}

if (IsOpen == false)
{
    var fmodel = new formModel();
    fmodel.MdiParent = this;
    fmodel.WindowState = FormWindowState.Minimized;
    fmodel.Show();
    fmodel.WindowState = FormWindowState.Maximized;
}

// Close another forms
foreach (Form frm in this.MdiChildren)
{
    if (!frm.Focused)
    {
        frm.Visible = false;
        frm.Dispose();
    }
}

Form2 is blank I tried the program click menu to open Form2 It's show successfully.

I inserted label1 in to Form2 and open Form2 It's show successfully.

but When I inserted textbox1 or button1 in to Form2 and open Form2 It's don't show. It's just blink.

Run the program

Who can tell me Why It be like this? Thank you.


Solution

  • This is wrong:

    f.Focus();
    

    and so is this:

    if (!frm.Focused)
    {
        frm.Visible = false;
        frm.Dispose();
    }
    

    They should be:

    f.Activate();
    

    and:

    if (frm != ActiveMdiChild)
    {
        frm.Close();
    }