Accessing MDI Forms in VB.NET (preventing multiple instances)


Thanks in advance for any assistance provided!

I have an application which I'm developing in VB.NET which uses forms in an MDI interface.  The main form is the container for virtually  all other forms in the application with the exception of some help and other simple notification forms.

In the main form, when the user clicks on a "button" to open a child form, I have placed the following code...

 If VSOpen = False Then

                VSOpen = True
                Dim frmView As New ViewStatus
                'Set the Parent Form of the Child window.
                frmView.MdiParent = Me
                'Display the new form.


                ' Code was removed due to errors.

            End If

I created a public shared variable in the main form called VSOpen in this case (boolean) which is set to true when the form is opened initially and is then set to False if the child form is closed.  My intention was to check the state of the variable and if it was false, create a new instance of the child form as shown above or if it was true, I intended to "Activate or otherwise show the instance of the form which was already open.

I have tried various ways to do this but keep running into the same issue.  Namely, when the child is closed the name of the form (frmView in this case) is disposed of and cannot be reopened.  I tried setting the dim statement which declares the form in the code above to a public shared variable and had the same issue.

Any suggestions would be most appreciated.  I need to ensure that one and only one instance of each form is open at any time but want to give the users the ability to click on the same controls to open and / or view them as needed (without having to resort to using the "Window - Active Windows" item in the main forms menu bar, which works fine by the way.

Who is Participating?
Mike TomlinsonMiddle School Assistant TeacherCommented:
Something like this will work:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Not loadMDIChild("ViewStatus") Then
            Dim f As New ViewStatus
            f.Name = "ViewStatus"
            f.MdiParent = Me
        End If
    End Sub

    Private Function loadMDIChild(ByVal childName As String) As Boolean
        Dim f As Form
        For Each f In Me.MdiChildren
            If f.Name.Equals(childName) Then
                If f.WindowState = FormWindowState.Minimized Then
                    f.WindowState = FormWindowState.Normal
                End If
                Return True
            End If
        Return False
    End Function
netdzynrAuthor Commented:
Thanks for responding to my question!  Both solutions would have worked but the one from worked best in my application.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.