Hello,
Within this sub I want to append test to a Richtextbox at the end of the sub (Rtb1):
Code:
Private Sub comPort_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
Dim str As String = String.Empty
Dim i As Integer
Dim bytecount As Integer = SerialPort1.BytesToRead
Dim byteBuffer(bytecount) As Byte
Dim DisplayInput As String = String.Empty
If bytecount > 0 Then
SerialPort1.Read(byteBuffer, 0, bytecount)
End If
i = 0
For i = 0 To bytecount 'Main for..Next loop to parse all data.
If Convert.ToChar(byteBuffer(i)) = Chr(126) Then
Do
DisplayInput = DisplayInput + Convert.ToChar(byteBuffer(i))
i = i + 1
Loop Until Convert.ToChar(byteBuffer(i)) = Chr(35)
'i = i + 1
DisplayInput = DisplayInput + Chr(35)
MsgBox(bytecount)
MsgBox("i: " & i)
MsgBox("Length: " & DisplayInput.Length.ToString)
MsgBox("String: " & DisplayInput.ToString)
Rtb1.AppendText(DisplayInput)
DisplayInput = String.Empty
End If
Next
i = 0
End Sub
But I get the following error:
System.InvalidOperationException was unhandled
Message = "You are not allowed to perform an operation through various threads, there was another thread from a given access to the control Rtb1 than the thread on which the element was created."
Why can I not append text to the Richtextbox in this sub? I do not use it anywhere else??
Thanks