Tamara Caligari
Tamara Caligari

Reputation: 509

Overwrite Visual Basic label text

I am reading a temperature value from a transceiver into the serial port and I want this value to change the value of a label in my Visual Basic Form. This value changes every few seconds. I am using the code below:

Me.dataReceived.Text &= [text]

where dataReceived is the label I am using and [text] is the data I am reading from the serial port. This results in the data being displayed but instead of overwriting the label, it writes the value after each other. (The data is appended). I tried to remove the & before the = but this did not work as nothing showed up. Any ideas on what I can do?

The code I am using is the following:

'To receive data into the text field
Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived

    While (SerialPort1.IsOpen)
        ReceivedText(SerialPort1.ReadExisting())    'This is called automatically every time  data is received at the Serial Port
    End While

End Sub

Private Sub ReceivedText(ByVal [text] As String)

    Dim temperature As String

    'This function compares the creating Thread's ID with the calling Thread's ID
    If Me.dataReceived.InvokeRequired Then
        Dim x As New SetTextCallback(AddressOf ReceivedText)
        Me.Invoke(x, New Object() {(text)})
    Else
        'To output to the text box:
        Me.dataReceived.Text = text

        'To output to the label
        temperature = text
        Me.temp.Text = text
        hmd.Text = temperature

    End If
   End Sub

Upvotes: 0

Views: 6131

Answers (4)

Steven Doggart
Steven Doggart

Reputation: 43743

Try this:

If (text IsNot Nothing) AndAlso (text.Trim().Length <> 0) Then
    Me.dataReceived.Text = text
End If

I'm not that familiar with the SerialPort class, but I'll do my best to explain what's going on. The serial port raises the data received event when new data comes in through the serial port. Your code then reads all the existing data that has been received. When you call the ReadExisting method, it only returns what has been received up to that point. It does not wait for all the data to come in before it returns. It then calls the ReceivedText method which sets the value of some controls to the text that was received. The InvokeRequired and Invoke calls are only there to get you back on the UI thread. Apparently the SerialPort's DataReceived event may be raised on a separate thread, so you need to get back to the UI thread before you can do anything to any of the controls on the form. The change I suggested simply checked to make sure that the text recieved was not null or empty before you changed the value of the text box. The odd thing about the code is that it continues to read from the serial port until it is no longer open. I wouldn't think you'd want to do that. Rather, I would think that in each DataReceived event, you would just call ReadExisting once and then just expect it to be raised again the next time more data is received. By continuously calling ReadExisting in a loop, it must either return a null string or an empty string if there is no more data to read, which was why the text box was being blanked out.

Upvotes: 1

Kratz
Kratz

Reputation: 4330

I'm not sure what is happening, but its likely that text does not have a value when setting the dataReceived.Text. Try this,

Private Sub ReceivedText(ByVal datatext As String)
     Me.Invoke(Sub()   
                    'To output to the text box:
                    Me.dataReceived.Text = datatext

                    Me.temp.Text = datatext
                    hmd.Text = datatext
               End Sub)
    End If
End Sub

I changed text to datatext since text could be confused with the forms local text property. I also moved the setting of the text properties of the labels/textboxes to an invoke using a lambda (note this syntax is new and only will work in VB 2010). I feel like somehow your invoke statement might not have been passing the string value properly. Also I removed the check for InvokeRequired, since the doing an invoke will work in both situations and it looks like you might just be doing threaded calls each time anyways.

If that compiles for you, it should work. If not, its likely that RecievedText is never being called. Set some breakpoints and step through the code to see that datatext has a value and that ReceivedText actually gets called.

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460158

If you want to overwrite the old value, you should not use &= but just = to assign the new value:

Me.dataReceived.Text = newText

&= is the same as Me.dataReceived.Text = Me.dataReceived.Text & newText

From MSDN:

The &= operator concatenates the String expression on its right to the String variable or property on its left, and assigns the result to the variable or property on its left.

Upvotes: 5

Amit
Amit

Reputation: 22086

&= is concatinating your text. use = to over write lable.

Me.dataReceived.Text = text

Upvotes: 0

Related Questions