I recently had cause to check whether I already had an instance of an MDIChild form open in VB.NET. Using Array.Exists on the MDIChildren property of the MDI Parent form seemed like an elegant way to go. However, after further investigation it looked like i’d need code some supporting functions for the predicate. What I went with in the end was this:
Dim formName As String = "" ' Enter form name here
For Each f As Form In Application.OpenForms
If f.IsMdiChild = True AndAlso f.Name = formName Then
' Yes, we have a MDI Child form open with the required name
End If
Next
Here’s the complete code to open a new form, or activate the existing form (if there is one) :
Dim formName As String = "" Enter form name here
Dim openForm As Form = Nothing
' Iterate and check for instance
For Each f As Form In Application.OpenForms
If f.IsMdiChild = True AndAlso f.Name = formName Then
openForm = f
End If
Next
openForm = CType(IIf(openForm Is Nothing, New OrderForm(New Guid(formName)), openForm), Form)
openForm.MdiParent = Me
openForm.Show()
openForm.Activate()