Click here to Skip to main content
15,881,709 members
Articles / Programming Languages / C#
Article

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

Rate me:
Please Sign up or sign in to vote.
3.64/5 (12 votes)
20 Jul 2004Public Domain 97.1K   20   16
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.

C#
/* 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.

C#
 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, along with any associated source code and files, is licensed under A Public Domain dedication


Written By
Software Developer (Senior) Intrepid Pursuits
United States United States
Presently living in Massachusetts, i am a Mobile developer specializing in iOS and Adroid, with previous work in the Windows .NET world and Linux GTK.

Comments and Discussions

 
GeneralAvoid to open more than one windows. Pin
V K Gupta25-May-09 23:11
V K Gupta25-May-09 23:11 
Generalmultiple child pages Pin
kanza azhar13-Aug-08 6:00
kanza azhar13-Aug-08 6:00 
QuestionActive form Pin
thachvv1813-Jul-07 3:48
thachvv1813-Jul-07 3:48 
AnswerRe: Active form Pin
Giorgi Dalakishvili13-Jul-07 4:22
mentorGiorgi Dalakishvili13-Jul-07 4:22 
GeneralRe: Active form Pin
gaurav_pandya29-Jul-08 19:35
gaurav_pandya29-Jul-08 19:35 
GeneralGood Pin
davarmanesh12-Jun-07 6:28
davarmanesh12-Jun-07 6:28 
Generalhelp... Pin
Gustavo Ushijima4-Mar-07 3:00
Gustavo Ushijima4-Mar-07 3:00 
QuestionUsing MDI Form Pin
m3n5428-Sep-06 0:52
m3n5428-Sep-06 0:52 
AnswerRe: Using MDI Form Pin
hhmiles214-Jan-07 17:38
hhmiles214-Jan-07 17:38 
GeneralRe: Using MDI Form Pin
Gulkit Jain17-Nov-07 3:24
Gulkit Jain17-Nov-07 3:24 
Questionaccess parent form of showgialog window Pin
amilapradeep25-Sep-06 2:42
amilapradeep25-Sep-06 2:42 
AnswerRe: access parent form of showgialog window Pin
amilapradeep25-Sep-06 3:32
amilapradeep25-Sep-06 3:32 
GeneralVersion 2.0 of this function Pin
javiersolis19837-Oct-05 3:44
javiersolis19837-Oct-05 3:44 
GeneralRe: Version 2.0 of this function Pin
jayoscar26-Feb-06 20:31
professionaljayoscar26-Feb-06 20:31 
GeneralNice but..... Pin
Blade200026-Apr-05 3:28
Blade200026-Apr-05 3:28 
This looks nice, but when I used it, it did not allow me to open another form in the MDI. In other words I was able to have only one Form in my MDI container....
Maybe I did something wrong, but I have modified the code a bit..... The new One:

internal bool CheckMdiClientDuplicates(string WndCls)
{
Form[] mdichld = this.MdiChildren;
if (this.MdiChildren.Length == 0)
{
return true;
}
else
{
foreach (Form selfm in mdichld)
{
string str = selfm.Name;
if (str.Equals(WndCls))
{
return false;
}
}
}
return true;
}

And In the If or switch statement, you send just the form without the namespace:

if (CheckMdiClientDuplicates("frmSearchData") == true)
{
frmSearchData search = new frmSearchData();
search.Show();
}
GeneralInverted return Pin
12-Nov-04 7:09
suss12-Nov-04 7:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.