2016-07-22 91 views
0

我正在使用visual basic 2010.我有以下格式的數據字符串。我想分割逗號分開的值並將其放入單獨的文本框中。For The last Temp_read:348 ,HV_Read:647,SPD:0,DIS:0」我想單獨拆分值並付諸文本框中分割字符串並將值放入文本框

可以有人建議我,我怎麼能做到這一點是有示例代碼

Public Class Form1 

    Dim selectedItem1 As String 
    Dim Data As String 
    Private Sub SMCB1_clientIP_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SMCB1_clientIP.TextChanged 

    End Sub 


    Private Sub SMCB1_Connect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SMCB1_Connect.Click 

     Dispay_Show.Text = SMCB1_clientIP.Text 
     Dispay_Show.Text = SMCB1_clientIP.Text & vbNewLine & SMCB1_Port.Text & vbNewLine 
     Data = "SMCB3,3,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,Temp_read:348,HV_Read:647,SPD:0,DIS:0" 
     Dispay_Show.Text = SMCB1_clientIP.Text & vbNewLine & SMCB1_Port.Text & vbNewLine & Data 
     Data.Split() 





    End Sub 


    Private Sub SMCB1_Disconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SMCB1_Disconnect.Click 

    End Sub 




End Class 

Image

Data =「SMCB3,3,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,Temp_read:348,HV_Read:647,SPD:0,DIS:0「

+0

你能指定你正在尋找你添加的圖像的預期輸出嗎? – Siva

回答

0

我寫了下面的代碼。有28個文本框。值應該被記錄到每個文本框中。用下面的代碼我可以記錄多達28個值。 但是從24放慢參數包含 Temp_read:348,HV_Read:647,SPD:0,DIS:0" 需要單獨的字符串 「:」,並把閱讀特定文本框

Option Explicit On 

Public Class Form1 

    Dim selectedItem1 As String 
    Dim Data As String 
    Dim WrdArray() As String 
    Dim line As String 

    Private Sub SMCB1_clientIP_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SMCB1_clientIP.TextChanged 

    End Sub 


    Private Sub SMCB1_Connect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SMCB1_Connect.Click 

     ' Dispay_Show.Text = SMCB1_clientIP.Text 
     'Dispay_Show.Text = SMCB1_clientIP.Text & vbNewLine & SMCB1_Port.Text & vbNewLine 
     Data = "SMCB3,3,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,Temp_read:348,HV_Read:647,SPD:0,DIS:0" 
     ' Dispay_Show.Text = SMCB1_clientIP.Text & vbNewLine & SMCB1_Port.Text & vbNewLine & Data 
     Dispay_Show.Text = Data 

     Dim strArray() As String 
     Dim intCount As Integer 
     Dim Tempr_read As String 
     Dim voltage As String 
     Dim SPD As String 
     Dim Dis_value As String 

     Data = "SMCB3,3,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,Temp_read:348,HV_Read:647,SPD:0,DIS:0" 
     strArray = Split(Data, ",") 
     SMCB1_Name.Text = strArray(0) 
     SMCB1_DeviceId.Text = strArray(1) 
     SMCB1_String1.Text = strArray(2) 
     SMCB1_String2.Text = strArray(3) 
     SMCB1_String3.Text = strArray(4) 
     SMCB1_String4.Text = strArray(5) 
     SMCB1_String5.Text = strArray(6) 
     SMCB1_String6.Text = strArray(7) 
     SMCB1_String7.Text = strArray(8) 
     SMCB1_String8.Text = strArray(9) 
     SMCB1_String9.Text = strArray(10) 

     SMCB1_String10.Text = strArray(11) 
     SMCB1_String11.Text = strArray(12) 
     SMCB1_String12.Text = strArray(13) 
     SMCB1_String13.Text = strArray(14) 
     SMCB1_String14.Text = strArray(15) 
     SMCB1_String15.Text = strArray(16) 
     SMCB1_String16.Text = strArray(17) 
     SMCB1_String17.Text = strArray(18) 
     SMCB1_String18.Text = strArray(19) 
     SMCB1_String19.Text = strArray(20) 

     SMCB1_String20.Text = strArray(21) 
     SMCB1_String21.Text = strArray(22) 
     SMCB1_String22.Text = strArray(23) 
     SMCB1_String23.Text = strArray(24) 
     SMCB1_String24.Text = strArray(25) 

     ' Tempr_read = Split(Data(strArray(26),":") 


     SMCB1_Temp.Text = strArray(26) 
     SMCB1_Hvread.Text = strArray(27) 
     SMCB1_SPD.Text = strArray(28) 
     SMCB1_DIS.Text = strArray(29) 


     For intCount = LBound(strArray) To UBound(strArray) 
      Debug.Print(Trim(strArray(intCount))) 
     Next 
    End Sub 



    Private Sub SMCB1_Disconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SMCB1_Disconnect.Click 

    End Sub 

End Class 
0

我建議使用InStrRev函數的兩個實例 - 一個用於冒號,另一個用逗號,同時向後循環穿過字符串並構建陣列。

通過這種方式,您將能夠捕獲右側的數據冒號(在逗號之前),但忽略了逗號和冒號之間的標籤(如果這實際上是你打算做的)。

請打我,如果你想一個工作的例子。

Regards

+0

筆畫。有沒有任何示例代碼 – Ajit