1
votes

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.

1
When you add something that is focusable, it's getting the focus rather than its parent, so your own code is immediately closing the form because it doesn't have focus.Damien_The_Unbeliever
This question has nothing to do with VB.NET. The description for the VS tag says that it's for IDE issues, not issues with code written in VS. Don't spam tags.jmcilhinney

1 Answers

0
votes

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();
}