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

Posted on 2004-11-11
Last Modified: 2010-04-24

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.

Question by:netdzynr
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    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
    LVL 7

    Accepted Solution


    Author Comment

    Thanks for responding to my question!  Both solutions would have worked but the one from worked best in my application.


    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
    It’s quite interesting for me as I worked with Excel using for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now