Link to home
Start Free TrialLog in
Avatar of D B
D BFlag for United States of America

asked on

Forms collection

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 vb.net. 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
    frm.SetFocus
    bFormFound = True
    Exit For
  End If
Next
If Not bFormFound Then
    ...create new instance here.
End If

How would I replicate this code in VB.Net 2005?
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

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
                frm.Focus()
                bFormFound = True
                Exit For
            End If
        Next
        If Not bFormFound Then
            Dim frm As New myForm()
        End If

Bob
ASKER CERTIFIED SOLUTION
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of D B

ASKER

Bob,
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.

Doug