I will create a more clearly view on my program:
CLIENT:
=======
Private Sub clientconnected_Load(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles MyBase.Load
Try
client = New TcpClient(ip, port)
client.GetStream.BeginRead(readBuffer,0,READ_BUFFER_SIZE, _
AddressOf DoRead, Nothing)
Catch Ex As Exception
MsgBox("Cannot Connect", MsgBoxStyle.Exclamation, "Error")
Me.Close()
End Try
End Sub
Private Sub DoRead(ByVal ar As IAsyncResult)
Dim BytesRead As Integer
Dim strMessage As String
Try
BytesRead = client.GetStream.EndRead(ar)
If BytesRead < 1 Then
MarkAsDisconnected("No butes read")
Exit Sub
End If
' Convert the byte array the message was saved into, minus
' two for the Chr(13) and Chr(10)
strMessage = Encoding.ASCII.GetString(readBuffer,0,_
BytesRead - 2)
Catch e As Exception
MarkAsDisconnected(e.Message)
Exit Sub
End Try
ProcessCommands(strMessage)
Try
client.GetStream.BeginRead(readBuffer, 0, READ_BUFFER_SIZE,_
AddressOf DoRead, Nothing)
Catch e As Exception
MarkAsDisconnected(e.Message)
End Try
End Sub
' Process the command received from the server,
' and take appropriate action.
Private Sub ProcessCommands(ByVal strMessage As String)
Dim dataArray() As String
' Message parts are divided by "|" Break the string
' into an array accordingly.
dataArray = strMessage.Split(Chr(124))
' dataArray(0) is the command.
Select Case dataArray(0)
Case "IMG"
DRAW_AN_IMAGE(dataArray(1))
Case Else
End Select
End Sub
Private Sub DRAW_AN_IMAGE(ByVal data As String)
Dim p As New PictureBox()
p.Image = New Bitmap("pics/" + data + ".bmp")
p.SetBounds(1, 1, 72, 96)
Panel.Controls.Add(p)
p.BringToFront()
End Sub
The error occurs on the "Panel.Controls.Add(p)" line
Any solutions ?
Thanks