title: Vb.net Network Programming cant add new user to listbox
body: <p>I'm starting to build TCP Server/Client and its protocol, but their are some problem that I could not understand because it less document about it, and it also something relate to cross-threading. I try to avoid illegal cross thread also. As far I do it work on my textbox to update the status from the raiseStatus event, anyway I don't know how to do it with listbox. Normally I cant send and receive all the message I want by using the protocol I build like this: LOGIN:NAK. It mean user NAK login, my program can communicate well with this protocol as I have more to implement. I just got stuck when adding NAK to the listbox1 while Client using this command.</p>
  
  <p><img src="https://i.stack.imgur.com/8SQsL.jpg" alt="My Server and Client testing image:"></p>
  
  <p>My Server Side Code:</p>
  
  <p>This is my form1 class</p>
  
  <pre><code>Public Class Form1
  
  Dim portToListen As Integer = 700
  Dim WithEvents manager As ClientMananger
  Dim WithEvents M_Client As Client
  
  Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
      manager = New ClientMananger(Me)
      manager.Listen(portToListen)
  
  End Sub
  
  Private Sub manager_Status(message As String) Handles manager.Status
      txtStatus.Text &amp;= message &amp; vbNewLine
  End Sub
  
  End Class
  </code></pre>
  
  <p>And This is my class ClientManager</p>
  
  <pre><code>Public Class ClientMananger
  
  Dim tcpListener As TcpListener
  Dim listenThread As Thread
  
  Dim clientList As New List(Of Client)
  
  Public Event Status(message As String)
  Public Event Connect(client As Client)
  Public Event Disconnect(client As Client)
  
  Public Sub Listen(port As Integer)
      tcpListener = New TcpListener(port)
  
      '' Start thread for listening for new income socket
      listenThread = New Thread(AddressOf ListenProc)
      listenThread.Start()
  End Sub
  
  Private Sub ListenProc()
      RaiseStatus("Listen to port ...")
  
      '' Continously listen to certain port
      tcpListener.Start()
      While True
          Dim incomeClient As New Client(tcpListener.AcceptSocket(), Me)
  
          '' Income connection has been made
          RaiseStatus("Client attempt to connect ...")
  
          '' Send welcome message to client
          incomeClient.Send("Welcome to RAMA Hospital Chat Server!")
          clientList.Add(incomeClient)
  
      End While
  End Sub
  
  Public Sub SendAll(client As Client, msg As String)
      For Each c As Client In clientList
          If client IsNot c Then
              c.Send(msg)
          End If
      Next
  End Sub
  
  Public Sub SendTo(sender As Client, rv As String, msg As String)
      For Each c As Client In clientList
          If c.name = rv Then
              c.Send("PRIVATE:" &amp; sender.name &amp; ":" &amp; msg)
          End If
      Next
  End Sub
  
  #Region "GUI"
  Dim form As Form
  
  '' GUI
  Public Sub New(form As Form)
      Me.form = form
  End Sub
  
  Private Sub RaiseStatus(txt As String)
      form.Invoke(RaiseStatusInvoker, txt)
  End Sub
  
  Private Delegate Sub RaiseStatusDelegate(txt As String)
  Private Sub RaiseStatusFunc(txt As String)
      RaiseEvent Status(txt)
  End Sub
  Private RaiseStatusInvoker As New RaiseStatusDelegate(AddressOf RaiseStatusFunc)
  
  #End Region
  
  End Class
  </code></pre>
  
  <p>And this is the Class Client that i cant make it work, is there anyway i can add my [part(1) mean username that split from the Client] part(1) to the listbox1? I used form1.listbox1.item.add(part(1)) but it not work it does not show any error also.</p>
  
  <pre><code>Public Class Client
  
  Dim sck As Socket
  Dim msgThread As Thread
  Public name As String = ""
  Dim parent As ClientMananger
  Private _socket As Socket
  Private _client As Client
  
  Public Event Status(message As String)
  
  Public Sub New(sck As Socket, parent As ClientMananger)
      Me.sck = sck
      Me.parent = parent
  
      '' Listen to incoming message
      msgThread = New Thread(AddressOf ReceiveMessage)
      msgThread.Start()
  End Sub
  
  Public Sub Send(message As String)
      Dim msgByte() As Byte = System.Text.ASCIIEncoding.ASCII.GetBytes(message)
      sck.Send(msgByte, SocketFlags.None)
  End Sub
  
  Private Sub ReceiveMessage()
      Try
          Dim buffer(1000) As Byte
          Dim size As Integer = 0
          Dim msg As String
  
          Do
              size = sck.Receive(buffer, SocketFlags.None)
              If size &gt; 0 Then
                  msg = System.Text.ASCIIEncoding.ASCII.GetString(buffer, 0, size)
                  HandleMessage(msg)
              End If
          Loop While size &gt; 0
      Catch ex As SocketException
          '' Disconnect
      End Try
  End Sub
  
  Private Sub HandleMessage(msg As String)
  
      Dim part() As String = msg.Split(":")
      Dim command As String = part(0).ToUpper()
  
      If command = "LOGIN" Then
          Me.name = part(1)
  
          **Form1.listbox1.item.add(part(1))**
  
  
          MessageBox.Show(part(1) + " Had been add to the server!!!")
      ElseIf command = "ALL" Then
          parent.SendAll(Me, part(1))
      ElseIf command = "PRIVATE" Then
          parent.SendTo(Me, part(1), part(2))
      End If
  
  End Sub
  End Class
  </code></pre>
  
  <p>M sorry for posting lot of codes but really cant make this work please help thx you in advance, if you want to get my client side code i can post here.</p>