try this it will solve your issue
if (ActiveMdiChild != null)
ActiveMdiChild.Close();
Ex:
suppose i have a MDIParent form and some child forms then on the click event of menu item the code will be as follows
private void testMenuToolStripMenuItem_Click(object sender, EventArgs e)
{
if (ActiveMdiChild != null)
{
ActiveMdiChild.Close();
ChildForm2 ch2 = new ChildForm2();
ch2 .MdiParent = this;
ch2 .Show();
}
}
and if u want to check out the name of the current Active form name before closing it then the following code will help you out :
private void testMenuToolStripMenuItem_Click(object sender, EventArgs e)
{
if (ActiveMdiChild != null)
{
if (ActiveMdiChild.GetType().Name != "ChildForm2")
{
ActiveMdiChild.Close();
ChildForm2 ch2= new ChildForm2();
ch2.MdiParent = this;
ch2.Show();
}
else
{
MessageBox.Show("Form is already open");
}
}
else
{
ChildForm2 ch2= new ChildForm2();
ch2.MdiParent = this;
ch2.Show();
}
}