?
Solved

Limit a MDI Child Form to only one instance

Posted on 2006-03-20
6
Medium Priority
?
811 Views
Last Modified: 2008-03-17
I have an MDI application with multiple child forms. Some of these child forms need to be limited to only one instance loaded at any given time. How would you do this and why?
0
Comment
Question by:bhlang
  • 3
  • 2
6 Comments
 
LVL 48

Accepted Solution

by:
jpaulino earned 1000 total points
ID: 16241059
Check is is there any open MDI form before open it. Something like this

For Each frm In frmParent.MdiChildren
   If frm.Name = frmName.Name Then Exit Sub
Next
0
 
LVL 5

Expert Comment

by:xersoft
ID: 16241138
This happens to me a lot. For example, say you are writing a program to edit product information and you choose to create a MDI style application. You do this because your users want to have multiple products open for editing at the same time. Of course you are going to create one form that has the ability to show a products information and allow editing of that information. Now it most cases (I’m sure not all) you won’t want your users to open the same product for editing twice in two different forms.

So here is what I do to stop this:

I create a form that will be used to edit products. In that form I create a public readonly property called ProductUniqueIdent.

My open routine goes in the MDI parent. In this case it will probably ask the user what product they wish to edit, possibly by showing them a list of editable products.

After the user choose a product they wish to edit I find the UniqueIdent for that product.

Then I scan all MDI child forms known to a MDI parent in a for/each loop.
        For Each child As Windows.Forms.Form In Me.MdiChildren

        Next

In this loop I check to see if the typeof child is of the ProductEditorForm’s type.
         If TypeOf child Is ProductEditorForm Then

         End If

After I have identified that the child in question is of the correct type I create a reference to that form like this:
      dim ProductEditor as ProductEditorForm = directcast(child, ProductEditorForm)

Then I check the unique identifier property on that form to see if it matches the one the user is trying to open:
                If UserChoosenIdent = ProductEditor.ProductUniqueIdent Then
                    Exit Sub
                End If

If you have not exited at the end of the loop, open a new product editor form in the normal way, ensuring you set the ProductUniqueIdent property of that new form to the value of the UserChoosenIdent.

That should do it.
0
 
LVL 5

Expert Comment

by:xersoft
ID: 16241147
Here is the entire function I wrote above:

    Private Sub OpenProductEditor(ByVal UserChoosenIdent As String)
        For Each child As Windows.Forms.Form In Me.MdiChildren
            If TypeOf child Is ProductEditorForm Then
                Dim ProductEditor As ProductEditorForm = DirectCast(child, ProductEditorForm)
                If UserChoosenIdent = ProductEditor.ProductUniqueIdent Then
                    Exit Sub
                End If
            End If
        Next

        'code to open a new form goes here
    End Sub
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:bhlang
ID: 16241182
Thanks. Here's my full solution for anyone else interested:
Private Sub MenuItem_Click
        Dim frm As Form
        For Each frm In Me.MdiChildren
            If frm.Name = frmName.Name Then
                Exit Sub
            End If
        Next
        Dim F As New frmName
        F.MdiParent = Me
        F.Show()
End Sub
0
 

Author Comment

by:bhlang
ID: 16241211
xersoft, I didn't see your solution before accepting the first one. Sorry mate.
0
 

Author Comment

by:bhlang
ID: 16241804
Here's an alternate method using xersoft's advice:
Private Sub MenuItem_Click
        Dim frm As Form
        For Each frm In Me.MdiChildren
            If TypeOf frm Is frmName Then
                Exit Sub
            End If
        Next
        Dim F As New frmCName
        F.MdiParent = Me
        F.Show()
    End Sub
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

831 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