简体   繁体   中英

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.

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)

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.

But make sure to take a look at this question and answer , as Plutonix suggests.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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