bhlang
asked on
Limit a MDI Child Form to only one instance
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?
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
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.ProductUniqu eIdent Then
Exit Sub
End If
End If
Next
'code to open a new form goes here
End Sub
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.ProductUniqu
Exit Sub
End If
End If
Next
'code to open a new form goes here
End Sub
ASKER
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
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
ASKER
xersoft, I didn't see your solution before accepting the first one. Sorry mate.
ASKER
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
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
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.ProductUniqu
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.