简体   繁体   English

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

[英]VB.NET how to check if a form with a specific text is opened

I have a form called Chatbox that i use for each contact that is clicked. 我有一个称为Chatbox的表单,可用于单击的每个联系人。

I do this with following code: 我使用以下代码执行此操作:

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

What i want to do is check --- 我想做的就是检查-

    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

Whats the best way to do this? 最好的方法是什么? (note: chatbox.name doesn't work) (注意:chatbox.name不起作用)

You can try: 你可以试试:

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

Application.OpenForms gets a collection of open forms owned by the application. Application.OpenForms获取该应用程序拥有的开放表单的集合。

But make sure to take a look at this question and answer , as Plutonix suggests. 但是,请务必按照Plutonix的建议看一下这个问题和答案

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

thanks to @VisualVincent AND @Plutonix 感谢@VisualVincent AND @Plutonix

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM