MDI Child表单调用,而不是生成

时间:2022-10-20 10:46:31

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.

我有一个带有3个嵌套子项的MDI表单。截至目前,它所能做的就是显示一个新表格。例如:每次按下菜单按钮,都会创建新的子表单(Form1)。现在,如果我在第二次或以后按下相同的菜单按钮,则会创建一个新的Form1,它将显示在上一个Form1上。

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.

我想要的是,每次触发事件处理程序(父窗体上的菜单item_click)而不是生成完全“新”的子窗体(弹出一个新窗口)时,它会调出相应的子窗体附在触发器上的。

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()

2 个解决方案

#1


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.

这样,第一次单击菜单项时,将创建一个新的InventoryForm1实例,之后,每次单击菜单项时,将重新打开原始实例。

#2


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

尝试以下(注意我有一段时间没有完成VB .Net,所以语法可能会关闭)

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

#1


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.

这样,第一次单击菜单项时,将创建一个新的InventoryForm1实例,之后,每次单击菜单项时,将重新打开原始实例。

#2


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

尝试以下(注意我有一段时间没有完成VB .Net,所以语法可能会关闭)

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