如果已打开,请将MDIChild表单置于前面

时间:2014-08-11 16:42:50

标签: vb.net forms mdichild bringtofront

我一直在努力让这个工作......我在MDIchild表单上有一个按钮,打开另一个MDIchild表单,但是如果表单已经打开,它就无法识别它并打开一个新表单而不是把它带到前面。这是我得到的代码:

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

    Dim MDIForm4 As New Form4
    MDIForm4.MdiParent = Me
    MDIForm4.Show()

End Sub

这适用于打开新表单的按钮,然后我尝试添加:

    If Not Form4 Is Nothing Then
        Form4.BringToFront()
    End If

但没有积极的结果。有人有什么想法吗?

此致

Jorge Brito

2 个答案:

答案 0 :(得分:4)

以下是我通常的做法:

For Each f As Form In Application.OpenForms
  If TypeOf f Is frmTest Then
    f.Activate()
    Exit Sub
  End If
Next

Dim myChild As New frmTest 
myChild.MdiParent = Me
myChild.Show()

请注意,这使用了Application.OpenForms,如果您只想要主窗体的子窗口,则可以使用Me.MdiChildren(假设Me =此MDI窗体)。

答案 1 :(得分:0)

现在修复!

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click


    For Each f As Form In Application.OpenForms
        If TypeOf f Is Form4 Then
            f.Activate()
            Exit Sub
        End If
    Next


    Dim MDIForm As New Form4
    MDIForm.MdiParent = Form2
    MDIForm.Show()

End Sub

我在错误的表单上定义了MDI父级!