Search code examples
c#duplicatesmdichild

Eliminating Possible Duplicate Of MDIChildren


What is wrong with my code for preventing Duplicate of MDI Children???

//Create a new instance of the MDI child template form
FAnalysis fanalysis = null;
if (fanalysis != null)
{
    fanalysis.WindowState = FormWindowState.Normal;
    fanalysis.Focus();
}
else
{
    fanalysis = new FAnalysis();
    fanalysis.MdiParent = this;
    //Display the child window
    fanalysis.Show();
    changeVisible(false, false, true, true, true, true);
 }

Any Help Would be Appretiated... Thanks


Solution

  • I would do something like:

            foreach(Form child in this.MdiChildren)
            {
                if (child is FAnalysis)
                {
                    if (child.WindowState == FormWindowState.Minimized)
                    {
                        child.WindowState = FormWindowState.Normal;
                    }
                    child.BringToFront();
                    return; // stop looking and exit the method
                }
            }
    
            // no match was found; create a new child:
            FAnalysis fanalysis = new FAnalysis();
            fanalysis.MdiParent = this;
            fanalysis.Show();
            changeVisible(false, false, true, true, true, true);