Forms collection

Posted on 2007-10-17
Last Modified: 2008-01-09
In VB6, there was a forms collection you could iterate to get a list of forms that were open in your application at runtime. I want to do something similar in I have an MDI application. Several of the forms can have multiple instances, but one in particular, I only want the user to be able to only create one instance. If the form has already been created, but has been minimized or is in the background of another child form, I want to restore and/or bring it to the front.
In VB6, I would do something like:

bFormFound = False
For Each frm In Forms
  If frm.Name = "myForm" Then
    If frm.WindowState = vbMinimized Then frm.WindowState = vbNormal
    bFormFound = True
    Exit For
  End If
If Not bFormFound Then
    ...create new instance here.
End If

How would I replicate this code in VB.Net 2005?
Question by:dbbishop
    LVL 96

    Expert Comment

    by:Bob Learned
    Try this:

            Dim bFormFound As Boolean = False
            For Each frm As Form In Application.OpenForms
                If frm.Name = "myForm" Then
                    If frm.WindowState = FormWindowState.Minimized Then frm.WindowState = FormWindowState.Normal
                    bFormFound = True
                    Exit For
                End If
            If Not bFormFound Then
                Dim frm As New myForm()
            End If

    LVL 55

    Accepted Solution

    if you have an mdi container just scan all its children:

    For Each frm in MDIChildren
       ... etcetera
    LVL 15

    Author Comment

    I am sorry, I did not see your answer. Notifications go to my personal email address and I usually shut down Outlook in the morning so I can access my personal email from work. I forgot to this morning and just so happened to get onto web mail before Outlook had grabbed Jamie's solution, so when I clicked on the link, I jsut saw his solution. In all fairness, had I noticed your post, I would have split the points. Also, in fairness, I probably still would have used his solution. Although all I have is the MDIParent and child forms, it does provide at least one less form in the collection that is being iterated than your solution does.

    I'll catch you next time.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
    Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    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…

    760 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

    7 Experts available now in Live!

    Get 1:1 Help Now