In my previous article I had shown you How to Generate SQL Server Database Scripts Automatically?, How to Open Command Prompt Using C#.net & VB.Net.
You may also like these articles like Connect Multiple Events to Single Event in Windows Forms, C# & VB.NET, Move or Add Multiple Selected Items Between ListBox Controls (C# & VB.NET), Remove Password/ Decrypt Excel Using C#, VB.Net in Windows Application, Verify if an Excel File is Password Protected/Encrypted in C#, VB.Net, How to Show Error & Warning MessageBox in C#.Net, Windows Application.
So, for this article first we will create a new windows application and add two forms. one form we will user as MDI and second form we will display inside the MDI parent. So first we will see how to make MDI form in windows application. Open property window for the form and by making IsMdiContainer form property true to get MDI form.
After this we will create a form 2 which we will open in the MDI container.
C#.Net
private void openForm2ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (CheckMdiClientDuplicatesForm("ProjectWindows.Form2") == true)
{
Form2 form2 = new Form2();
form2.MdiParent = this;
form2.Show();
}
}
public bool CheckMdiClientDuplicatesForm(string windowsFormName)
{
if (ActiveMdiChild != null)
{
if (ActiveMdiChild.Name != windowsFormName.Split('.')[1].ToString())
{
ActiveMdiChild.Close();
}
}
Form[] mdichld = this.MdiChildren;
if (this.MdiChildren.Length == 0)
{
return true;
}
foreach (Form selfm in mdichld)
{
string str = selfm.Name;
str = str.IndexOf(windowsFormName).ToString();
if (str != "-1")
{
return true;
}
}
return false;
} VB.Net
Class SurroundingClass
Private Sub openForm2ToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs)
If CheckMdiClientDuplicatesForm("ProjectWindows.Form2") = True Then
Dim form2 As Form2 = New Form2()
form2.MdiParent = Me
form2.Show()
End If
End Sub
Public Function CheckMdiClientDuplicatesForm(ByVal windowsFormName As String) As Boolean
If ActiveMdiChild IsNot Nothing Then
If ActiveMdiChild.Name <> windowsFormName.Split("."c)(1).ToString() Then
ActiveMdiChild.Close()
End If
End If
Dim mdichld As Form() = Me.MdiChildren
If Me.MdiChildren.Length = 0 Then
Return True
End If
For Each selfm As Form In mdichld
Dim str As String = selfm.Name
str = str.IndexOf(windowsFormName).ToString()
If str <> "-1" Then
Return True
End If
Next
Return False
End Function
End Class In above code i have created a method and in this method i have add that if ActiveMdiChild is not null. After that we are validating id the current passed form is open or not. If it is open dent do anything otherwise open the form. Now we have done lets run the code to heck the output.

