Check for open mdi child, with focus (C#)

This code snippet shows you how to check if is your MDI container has a specific MDI child open or not.
The sample below looks though each of the mdi children in the container for a instance of the form you want to open. If an instance is found, the form is brought to the front (focused). If an instance of the form is not found, the code opens a new instance of the form.

Snippet
[code lang=”csharp”]
private void CheckMdiChildren(Form form)
{
     foreach (Form frm in this.MdiChildren)
     {
          if (frm.GetType() == form.GetType())
          {
               frm.Focus();
               return;
          }
     }

     form.MdiParent = this;
     form.Show();
}
[/code]

Usage:
[code lang=”csharp”]
MyForm form = new MyForm();
CheckMdiChildren(form);
[/code]

Revision Log
1.0 – Initial release