首页 > 解决方案 > 通过找出一个表单类是否已经打开,只打开一次表单

问题描述

我有一个菜单列表,其中包含一些打开接受参数的表单的菜单项。目前,当它打开表单时,它将创建另一个表单,而不是关注已经打开的表单。

我看过很多 C# 示例,但很难转换它们。

我已经尝试过这段代码,但我认为它不起作用,因为尽管菜单表单是 mdiContainer 表单,但它打开的表单不是子表单。我已将其作为我想要查找的内容,即打开的特定 CLASS 表单。

    For Each child In Me.MdiChildren
        If TypeOf child Is frmCustomerPurchaseOrders Then
            child.WindowState = FormWindowState.Normal
            child.Focus()
            Exit Sub
        End If
    Next

    Dim myForm As New frmCustomerPurchaseOrders("NotFullyInvoiced")
    myForm.Show()

我查看了 My.Application.OpenForms ,它确实获取了表单文本,但是随着表单文本在打开时发生变化,很难通过表单名称进行匹配。有没有办法检查特定类别的表格是否已经打开?

        For Each f As Form In My.Application.OpenForms
            MessageBox.Show(f.Text)
        Next

但是,如果我走错了路,请告诉我!非常感谢

标签: vb.net

解决方案


你可以用 OpenForms 做同样的事情,就像你的示例代码用 MdiChildren 做的一样;f只需检查循环中每个 Form, 的类型:

For Each f As Form In My.Application.OpenForms
    If TypeOf f Is frmCustomerPurchaseOrders Then
        ' ... do something in here with "f" ...
    End If
Next

推荐阅读