1

I've made a UserControl (with the help of Plutonix) but i have a little problem to change these objects from status.

I dynamically create them with this code:

While UserData.Read

                Dim CPID As String
                Dim CPUN As String
                CPID = UserData("Username").ToString
                CPUN = UserData("Voornaam").ToString & " " & UserData("Achternaam").ToString

                Dim CP As New Contacts(CPID, CPUN)
                CP.Name = CPID
                CP.ContactName.Name = CPID
                AddHandler CP.ContactName.Click, AddressOf Chatbox
                If UserData("Status").ToString = "Online" Then
                    CP.Status = Contacts.ChatStatus.Online
                    If UserData("NieuwBericht").ToString = "Ja" Then
                        CP.MsgStatus = Contacts.ChatMsgStatus.Ja
                    ElseIf UserData("NieuwBericht").ToString = "Nee" Then
                        CP.MsgStatus = Contacts.ChatMsgStatus.Nee
                    Else
                        CP.MsgStatus = Contacts.ChatMsgStatus.Onbekend
                    End If
                ElseIf UserData("Status").ToString = "Afwezig" Then
                    CP.Status = Contacts.ChatStatus.Afwezig
                    If UserData("NieuwBericht").ToString = "Ja" Then
                        CP.MsgStatus = Contacts.ChatMsgStatus.Ja
                    ElseIf UserData("NieuwBericht").ToString = "Nee" Then
                        CP.MsgStatus = Contacts.ChatMsgStatus.Nee
                    Else
                        CP.MsgStatus = Contacts.ChatMsgStatus.Onbekend
                    End If
                ElseIf UserData("Status").ToString = "Offline" Then
                    CP.Status = Contacts.ChatStatus.Offline
                    If UserData("NieuwBericht").ToString = "Ja" Then
                        CP.MsgStatus = Contacts.ChatMsgStatus.Ja
                    ElseIf UserData("NieuwBericht").ToString = "Nee" Then
                        CP.MsgStatus = Contacts.ChatMsgStatus.Nee
                    Else
                        CP.MsgStatus = Contacts.ChatMsgStatus.Onbekend
                    End If
                Else
                    CP.Status = Contacts.ChatStatus.Onbekend
                    If UserData("NieuwBericht").ToString = "Ja" Then
                        CP.MsgStatus = Contacts.ChatMsgStatus.Ja
                    ElseIf UserData("NieuwBericht").ToString = "Nee" Then
                        CP.MsgStatus = Contacts.ChatMsgStatus.Nee
                    Else
                        CP.MsgStatus = Contacts.ChatMsgStatus.Onbekend
                    End If
                End If

                CP.Dock = DockStyle.Top
                ChatContactPanel.Controls.Add(CP)

            End While

As result i get this:

enter image description here

Now my question is: How can i change the MsgStatus and Status of the contacts object from every user

Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
Thomas Dutoit
  • 129
  • 1
  • 12

2 Answers2

2

Assuming you want to find "Patricia":

Dim user = "Patricia"

Dim chatter = ChatContactPanel.Controls.
                   OfType(Of Contacts).
                   FirstOrDefault(Function(c) c.ChatName.StartsWith(user))
If chatter IsNot Nothing Then
    chatter.Status = chatter.ChatStatus.Online
End If

I am guessing "Contacts" is the user control name, and "ChatContactPanel" is a container control where they are stored. The original answer has an edit with this info.

  • It would be better to find them by Id because names are rarely unique
  • It would also be even better for a ChatUser class to manage it rather than finding them each time something changes.

Surely there is other runtime data your app tracks by user. A ChatControl property in a ChatUser class would prevent you from having to search for them at all and allow the class to manage it:

myChatBox.Status = myStatus

The class could also create the control for you with the properties already set.

Community
  • 1
  • 1
Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
1

I know this isn't an answer to your question - This is just a helpful pointer - please feel free to let me know if you want me to delete this - and apologies if I've missed something, and if there are typos in my code, but your big block of If..End If statements contains a lot of repeated code. It could be rewritten like this -

Select Case Userdata("Status").ToString
    Case "Online"
        CP.Status = Contacts.ChatStatus.Online
    Case "Afwezig"
        CP.Status = Contacts.ChatStatus.Afwezig
    Case "Offline"
        CP.Status = Contacts.ChatStatus.Offline
    else
        CP.Status = Contacts.ChatStatus.OnBekend
    End Select

    If UserData("NieuwBericht").ToString = "Ja" Then
        CP.MsgStatus = Contacts.ChatMsgStatus.Ja
    ElseIf UserData("NieuwBericht").ToString = "Nee" Then
        CP.MsgStatus = Contacts.ChatMsgStatus.Nee
    Else
        CP.MsgStatus = Contacts.ChatMsgStatus.Onbekend
    End If

Also apologies if the code is completely wrong, but I'm not able to test it.

David Wilson
  • 4,369
  • 3
  • 18
  • 31
  • That was two-thirds of the point I was trying to make with the [original answer](http://stackoverflow.com/a/35872915/1070452) even with a DRY link part of the point was lost. Even better would be if the DB and UC used the same things for the states – Ňɏssa Pøngjǣrdenlarp Mar 15 '16 at 23:58