vb.net 如果已经打开,将 MDIChild 表单放在前面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25248253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 18:01:56  来源:igfitidea点击:

Bring MDIChild form to front if already open

vb.netformsmdichildbringtofront

提问by OMGSOMETAL

I've been struggling to get this to work...I have a button on a MDIchild form that opens another MDIchild form, but if the form is already open, it does not recognize it and opens a new one instead of bringing it to front. This is the code i've got:

我一直在努力让它工作......我在 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

This works for the button to open the new form, and then I tried adding this:

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

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

But with no positive outcome. Does someone have any ideas?

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

Regards,

问候,

Jorge Brito

豪尔赫·布里托

回答by Steve

Here is how I typically do that:

这是我通常的做法:

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

Notice this uses Application.OpenForms, you can use your Me.MdiChildren (assuming Me = this MDI form) if you want just the children of your main form.

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

回答by OMGSOMETAL

Fixed now!

现在固定!

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

I was defining the MDI parent on the wrong form!

我在错误的形式上定义了 MDI 父级!