MDI 子窗体调用,而不是生成

发布于 2024-07-25 22:31:46 字数 600 浏览 6 评论 0原文

我有一个 MDI 表单,其中有 3 个嵌套子项。 截至目前,它所能做的就是显示一个新表单。 例如:每次按下菜单按钮时,都会创建新的子表单(Form1)。 现在,如果我再次或随后按下同一菜单按钮,则会创建一个 Form1,并且它会显示在前一个窗体上。

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

我想这就像重用一个对象一样。

任何帮助将不胜感激。

这是我正在使用的代码示例:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

奶气 2024-08-01 22:31:46

我认为您在这里想要的是表单的类级别变量。 类似 -

'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

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

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.

白昼 2024-08-01 22:31:46

尝试以下操作(请注意,我有一段时间没有做过 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文