0

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)

Thomas Dutoit
  • 129
  • 1
  • 12

2 Answers2

1

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.

Community
  • 1
  • 1
Stijn
  • 1,970
  • 3
  • 27
  • 36
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

thanks to @VisualVincent AND @Plutonix

Thomas Dutoit
  • 129
  • 1
  • 12
  • Okay, removing the form when closing. Do you target .NET 4.0 or higher? If so you could do like this: `AddHandler ChatBoxWindow.FormClosed, Sub() Chat_WindowList.Remove(ChatBoxWindow)`. – Visual Vincent Mar 08 '16 at 16:29
  • I did this @visualvincent and does the same :): `Sub Chatbox_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing Dim thisOne = Chatsys.Chat_WindowList.FirstOrDefault(Function(x) x.Name = ChatRxUsername) Chatsys.Chat_WindowList.Remove(thisOne) End Sub` – Thomas Dutoit Mar 08 '16 at 16:41
  • Skip the variables and stuff in the `Closing` sub. Do it in one line: `Chatsys.Chat_WindowList.Remove(DirectCast(sender, ChatBox))` – Visual Vincent Mar 08 '16 at 16:56