VB.NET如何检查是否打开了具有特定文本的表单

时间:2016-03-08 14:37:17

标签: vb.net

我有一个名为Chatbox的表单,我用它来点击每个联系人。

我使用以下代码执行此操作:

    Dim ChatBoxWindow As New Chatbox
    labelhandlename = DirectCast(sender, Label).Name
    ChatBoxWindow.Name = labelhandlename
    Chat_WindowList.Add(ChatBoxWindow)
    ChatBoxWindow.Show()

我想做的是检查---

    Sub Chatbox(sender As System.Object, e As System.EventArgs)

    labelhandlename = DirectCast(sender, Label).Name
    Dim thisOne = Chat_WindowList.FirstOrDefault(Function(x) x.Name = labelhandlename)

    If Chatbox.name = labelhandlename Then

        thisOne.Focus()

    Else

        Dim ChatBoxWindow As New Chatbox
        ChatBoxWindow.Name = labelhandlename
        Chat_WindowList.Add(ChatBoxWindow)
        ChatBoxWindow.Show()

    End If

End Sub

这是最好的方法吗? (注意:chatbox.name不起作用)

2 个答案:

答案 0 :(得分:1)

您可以尝试:

For Each myForm As Form In Application.OpenForms
    If myForm.Name = "something" Then
        ' Do something.
    Else
        ' Do something else.
    End If
Next
  

Application.OpenForms获取应用程序拥有的开放表单集合。

但请务必查看this question and answer,如Plutonix所示。

答案 1 :(得分:0)

Sub Chatbox(sender As System.Object, e As System.EventArgs)

    labelhandlename = DirectCast(sender, Label).Name
    Dim thisOne = Chat_WindowList.FirstOrDefault(Function(x) x.Name = labelhandlename)

    If thisOne IsNot Nothing Then

        thisOne.Focus()

    Else

        Dim ChatBoxWindow As New Chatbox
        ChatBoxWindow.Name = labelhandlename
        Chat_WindowList.Add(ChatBoxWindow)
        ChatBoxWindow.Show()

    End If

End Sub

感谢@VisualVincent和@Plutonix