Open Form MDI Parent Using C#.Net and VB.Net | Open Only One Form in MDI in C#

How to create a MDI form in windows application using C#.net & vb.net. Open form only ones to avoid duplicate form in MDI form using c#.net.
In today's article I will show you how you can how you can open a form in MDI parent using C#.net and VB.Net. So here we will open a form in MDI parent with control box having MinimizeBox, ManimizeBox  and button for close. 


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.

MDI container property

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.

Open form only ones in MDI parent
Open For MDI Parent Using C#.Net and VB.Net.zip 200KB

Post a Comment