Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Check to see if an MDI Child is already active in an MDI Parent

0.00/5 (No votes)
20 Jul 2004 1  
A Simple boolean function to check if an MDI Child has already been loaded into an MDI Parent Container.

Introduction

This is a simple to implement function to determine if an MDI child has already been loaded into the MDI parent. You can then use a Switch, Select Case, or If Statement to choose what to do.

Background

I needed a solution to detect if an MDI Child Form has already been loaded into it's Parent Container. After some searching online, I found nothing.

Function Code

Place the following in your MDI Parent's form Class.

/* the String WndCls is the windows full path. Namespace.Classname */
internal bool CheckMdiClientDuplicates(string WndCls)
{
 Form[] mdichld= this.MdiChildren; 
 if (this.MdiChildren.Length == 0) 
 {
  return true;
 }
 foreach (Form selfm in mdichld) 
 {
  string str=selfm.Name;
  str = str.IndexOf(WndCls).ToString();
  if (str != "-1")
  {
   return true;
  }
 }
  return false;
}

Implementation

The following checks to see if the About MDI Child Form has been created. If it hasn't, it then creates it.
The Namespace for this Application is BTs and the Class Name for the About Form is AboutWnd.

 if (CheckMdiClientDuplicates("BTs.AboutWnd") == true){
 AboutWnd AboutWindow = new AboutWnd(this);
 AboutWindow.Show();
 }

Summary

Again, I didn't find anything on this with a quick search online. I've tried MSDN, Google and several others. It was time sensitive and imperative that this gets done and I hadn't the time to continue looking. However this function seems to work well and quickly.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here