for a chat client i want to create a new form instance for each user who is writing a private message and check if the user has an open instance already or not before creating an instance.
i tried to check it using dictionary but i could not get it to work..
Dim convs As New Dictionary(Of String, String)
Dim nf As New FrmDialog()
convs.Add(WritePUser, WritePMessage)
nf.Show()
If convs.ContainsKey(WritePUser) Then
MsgBox("im already open")
Else
nf.Show()
End If
It's hard to know whether the code you posted is in one place in your app or multiple but it doesn't make sense to declare and create the
Dictionarywhere you use it. If you do that then you'll be creating a newDictionaryevery time so you'll never be able to see previously-created forms. You need to create oneDictionaryand assign it to a field, i.e. a class-level variable. You then use that one and onlyDictionaryevery time. The values in theDictionaryshould also be forms, notStrings, so that you can access the existing form. You also need to either remove forms from theDictionarywhen they close or else check whether the existing form is disposed. The latter is probably the simpler option.Then: