65.9K
CodeProject is changing. Read more.
Home

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

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.64/5 (12 votes)

Jul 21, 2004

Public Domain
viewsIcon

98507

downloadIcon

2

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.