另一个Form的WinForm Child,但是在MDI容器中

时间:2013-06-02 20:11:51

标签: .net winforms

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

我尝试将'Sub Form Child'父级设置为'Sub Form',将其.MdiParent设置为Main form但是它不会让我。

我将如何实现这一目标?

1 个答案:

答案 0 :(得分:1)

创建它时,只需保留对“Sub Form Child”的引用,然后在“Sub Form”的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