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.
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.