Error with VB2010

Hello,
I have the Arduino send values back to VB2010 and depending on the value, it will be displayed in a different label.
Below is my VB code:

    Sub EventHandler(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived

        Dim datareceived As Boolean = False
        Dim recStr As String = ""

        While Not datareceived

            Dim recVal As Integer
            recVal = SerialPort1.ReadByte()
            If recVal = 10 Or recVal = 13 Then
                datareceived = True
            Else
                recStr = recStr + Chr(recVal)
            End If

        End While

        If recStr <> "" Then
            If recStr > 1000 Then
                recStr = recStr - 1000
                Label10.Text = recStr
            End If

            If recStr > 100 And recStr < 1000 Then
                recStr = recStr - 100
                Label4.Text = recStr
            End If

            If recStr < 0 Then
                recStr = recStr * (-1)
                recStr = recStr - 10
                Label8.Text = recStr
            End If

            If recStr > 0 And recStr < 10 Then
                recStr = recStr
                Label6.Text = recStr
            End If
            If recStr > 10 And recStr < 100 Then
                recStr = recStr - 10
                Label8.Text = recStr
            End If




        End If

    End Sub

If I use a message box then the variable is displayed correctly. but when I try to display the variable on a label I get an error.
Anybody knows why??

Thanks for your help.

but when I try to display the variable on a label I get an error.

And that error is?

            If recStr > 1000 Then

But, recStr is a String. What does a String greater than 1000 mean?

I think you need to convert the String to a number, if possible, and compare and manipulate that number.

I thought VB (particularly in later versions) let you do things like that.

yes it does, but you have to insert an error check, otherwise the program will error if you type anything that is not a number…

i tried it with a button instead of datareceived and it all worked fine, so i don’t see why it shouldn’t work with datareceived
It changes the labeltext as you want, and does not throw any error

Here is the error that I get: "Cross-thread operation not valid: Control 'Label4' accessed from a thread other than the thread it was created on."

With a button it works fine! but the way I want to use, it does not work at all!

Any ideas??

Any ideas??

Sure. The error message is telling you EXACTLY what is wrong.

"Cross-thread operation not valid: Control 'Label4' accessed from a thread other than the thread it was created on."

The thread that does the serial reading has to Invoke() the thread that manages the GUI. Hit the help pages for how to do this in VB. I could tell you for C#, but that wouldn't help you.

did you manage to solve this problem already?

You need to check up on using delegates because the code that handles the serial comm. is running in another thread than he code updating the UI.

This might point you in the right direction:

mikmo.dk/misc/vbarduino.zip

Not the most elegant code, but it works.