Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 690
  • Last Modified:

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?
0
dbbishop
Asked:
dbbishop
1 Solution
 
Bob LearnedCommented:
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
0
 
Jaime OlivaresSoftware ArchitectCommented:
if you have an mdi container just scan all its children:

For Each frm in MDIChildren
   ... etcetera
0
 
dbbishopAuthor Commented:
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
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now