Profile

Sabtu, 26 Maret 2011

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

private void CheckMdiChildren(Form form)
{
foreach (Form frm in this.MdiChildren)
{
if (frm.GetType() == form.GetType())
{
frm.Focus();
return;
}http://www.blogger.com/img/blank.gif
}

form.MdiParent = this;
form.Show();
}

Usage:
MyForm form = new MyForm();
CheckMdiChildren(form);