1

我有一个主窗体,它是一个 MDI 容器,然后是另一个窗体(我们称之为子窗体),它在主窗体容器中打开。但是,当我按下子表单上的某个按钮并且它存在于主表单容器空间中时,我希望另一个表单(子表单子)打开,但我也希望它是子表单的子表单,所以如果我关闭子表单,它也会关闭“子表单子”

我尝试将“子表单子”父级设置为“子表单”并将其 .MdiParent 设置为主表单,但它不会让我这样做。

我将如何实现这一目标?

4

1 回答 1

1

只需在创建时保留对“子表单子”的引用,然后在“子表单”的 FormClosing() 事件触发时将其关闭:

Public Class SubForm

    Private _SubFormChild As SubFormChild = Nothing

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        If IsNothing(_SubFormChild) OrElse _SubFormChild.IsDisposed Then
            _SubFormChild = New SubFormChild
            _SubFormChild.MdiParent = Me.MdiParent
            _SubFormChild.Show()
        Else
            If _SubFormChild.WindowState = FormWindowState.Minimized Then
                _SubFormChild.WindowState = FormWindowState.Normal
            End If
            _SubFormChild.Activate()
        End If
    End Sub

    Private Sub SubForm_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If Not IsNothing(_SubFormChild) AndAlso Not _SubFormChild.IsDisposed Then
            _SubFormChild.Close()
        End If
    End Sub

End Class
于 2013-06-02T20:36:43.563 回答