我正在从收发器读取温度值到串口,我希望这个值改变我的Visual Basic窗体中的标签的值.该值每隔几秒就会改变一次.我使用下面的代码:
Me.dataReceived.Text &= [text]
Run Code Online (Sandbox Code Playgroud)
dataReceived是我正在使用的标签,[text]是我从串口读取的数据.这会导致显示数据,但不会覆盖标签,而是将值相互写入.(附加数据).我试图在=之前删除&但是这没有用,因为没有出现.关于我能做什么的任何想法?
我使用的代码如下:
'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
Run Code Online (Sandbox Code Playgroud)
如果要覆盖旧值,则不应使用&=而只是=分配新值:
Me.dataReceived.Text = newText
Run Code Online (Sandbox Code Playgroud)
&= 是相同的 Me.dataReceived.Text = Me.dataReceived.Text & newText
来自MSDN:
的
&=运算符连接在其上其左String变量或产权字符串表达式,并将结果指定给在其左侧的变量或属性.