views:

439

answers:

2

Hello.

I have an MDI form with 3 nested children with in it. As of right now all it can do is display a new form. For example: each time I press the menu button, the new child form(Form1) is created. Now, if I press that same menu button a second or subsequent time a new Form1 is created and it appears over the previous one.

What I would like is that each time the event handler is triggered (a menu item_click on the parent form) that instead of a completely "new" child form being produced(a new window popping up) it would instead pull up the appropriate child form that is attached to the trigger.

I suppose it would be something like reusing an object.

Any help would be greatly appreciated.

Here's the code sample I'm using:

Private Sub RadMenuItem1_Click(ByVal sender As System.Object, ByVal e As_
System.EventArgs) Handles RadMenuItem1.Click 
Dim NewMDIChild As New InventoryForm1()
'Set the Parent Form of the Child window.'
NewMDIChild.MdiParent = Me
'Display the new form.'
NewMDIChild.Show()
A: 

Try the following (note that I haven't done VB .Net in a while, so the syntax may be off)

Dim ChildInstances As New Dictionary(Of RadMenuItem, Form)

Private Sub RadMenuItem1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles RadMenuItem1.Click 
    Dim ChildForm As Form

    If Not ChildInstances.TryGetValue(RadMenuItem1, Out ChildForm) Then
        Set ChildForm = New InventoryForm1()
        ChildForm.MdiParent = Me
        ChildInstances.Add(RadMenuItem1, ChildForm)
    Else If ChildForm.Disposed Or Not ChildForm.Visible Then 'The user closed the form
        Set ChildForm = New InventoryForm1()
        ChildForm.MdiParent = Me
        ChildInstances(RadMenuItem1) = ChildForm
    End If

    ChildForm.Show()
End Sub
SLaks
+1  A: 

I think what you want here is a class level variable for the form. Something like -

'Class level (outside of a method)
Dim NewMDIChild As InventoryForm1

Private Sub RadMenuItem1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles RadMenuItem1.Click

if (NewMDIChild Is Nothing) Then
  NewMDIChild= New InventoryForm1
  NewMDIChild.MdiParent = Me
End if

newFrm.Show()
End Sub

That way, the first time the menu item is clicked, a new instance of InventoryForm1 will be created, after that, each time the menu item is clicked the original instance will be re-opened.

Ben
Static is the wrong term here.
SLaks
Sorry, you are right, it doesnt necessarily have to be a static (shared in Vb) variable. (although it would still work)
Ben
Unless you have multiple instances of the MDI parent (which is, I admit, unlikely)
SLaks
In which case you would need a global variable and not a class (form in this case) level variable anyway, so neither a shared or non shared variable would work in this example.
Ben
Thanks again for the comments. Under the Private Sub I just tried: InventoryForm1.MdiParent = Me'_InventoryForm1.Show() This seems to work insofar as when I hit the menu button once it only brings up the form once (which is what I ultimately want). What I also dont know is if this(my new snippet) will be good for the app as a whole
What do you mean by "Good for your app as a whole"?The one thing you wont be able to do is Dispose of your Dialog once you've closed it (as your using Show() not ShowDialog() (showdialog being modal meaning you know when you've finished with it, so you can dispose of it in a finally block)).
Ben
When I said "[will it be]good for the app as a whole" I meant basically what you just explained. My method was more of the "quick fix" but as you just showed it isn't the best or the one that would serve the app the best. The old adage of "the quick fix isnt always the best fix". Thanks for pointing out that bit of functionality that I would have issues with.