Vb.net Winforms MDI多个表单打开实例

时间:2015-11-16 04:56:28

标签: vb.net forms mdi

我想知道如何在MDI父表单中打开多个表单而不重复相同的实例? 例如:许可表格和用户表格同时打开,没有重复。

       For Each f As Form In Application.OpenForms
            If f.Name = "License" Then
                FormOpen = True
                f.Focus()
                Return
            End If

            If f.Name = "User" Then
                FormOpen = True
                f.Focus()
                Return
            End If
        Next

        If FormOpen = False Then
            If e.Node.Name = "License" Then
                Dim license As New License
                license.MdiParent = Me
                license.Show()
            End If


            If e.Node.Name = "User" Then
                Dim license As New User
                license.MdiParent = Me
                license.Show()
            End If
        End If

这是我的代码,但它只能同时打开一个表单。有人可以帮忙吗?

2 个答案:

答案 0 :(得分:1)

您可以在模块中声明一个公共布尔变量,以避免每次打开表单时出现重复。如果您成功打开变量存储" True"值;如果它不存储错误。你还必须存储"错误"到每个表单的Closed事件中的变量。

下面是源代码:

Private Sub MDIParent1_Load(sender as Object,e As EventArgs)处理MyBase.Load         User.MdiParent =我         License.MdiParent =我     结束子

Private Sub OpenLicenseAndUserFormsToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OpenLicenseAndUserFormsToolStripMenuItem.Click
    If bool = True Then
        Exit Sub
    End If
    Try
        For Each f As Form In Me.MdiChildren
            If f.Name = "License" Then
                Dim frm As New License()
                frm.MdiParent = Me
                frm.Show()

            End If

            If f.Name = "User" Then
                Dim frm As New User()
                frm.MdiParent = Me
                frm.Show()

            End If
            bool = True
        Next
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try

End Sub

答案 1 :(得分:0)

使用Exit语句代替Return

Exit语句退出过程或块,并立即将控制转移到过程调用或块定义之后的语句。

Return语句将控制权返回给调用FunctionSubGetSetOperator过程的代码。< / p>

在这种情况下,请使用Exit For