none
Split string then put in textbox RRS feed

  • Question

  • Hi All,

    How to do,

    I have Datagridview.In Column "Elem" having string "Ts".When loop through Rows then find Value of "Reported" put into TextBox.

     Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
            For i = 0 To 15
                If CStr(DataGridView1.Rows(i).Cells("Elem").Value) = "Ts" Then
                    Dim s As String = CStr(DataGridView1.Rows(i).Cells("Reported ").Value)
    
                    ' Split the string on the backslash character.
                    Dim parts As String() = s.Split(New Char() {CChar("["), "]"c}, StringSplitOptions.RemoveEmptyEntries)
    
                    For count = 0 To parts.Length - 1
    
    
                        TextBox9.Text = (parts(count)) 'First Find Ts
                        TextBox10.Text = (parts(count)) 'Second Find Ts
                        TextBox11.Text = (parts(count)) 'third Find Ts
                        TextBox12.Text = (parts(count)) 'Fourth Find Ts
    
                    Next
    
                End If
            Next
    
        End Sub

     
    Friday, March 18, 2016 1:53 PM

Answers

  • 158.4

    Then the split statement is not going to give you the 3 array elements that the code expects.

    You need to add code that allows for the possibility that a field is '158.4' and not '[158.4]'.

    Or, don't use Split.  String.Replace would work, as would Trim.

    https://msdn.microsoft.com/en-us/library/fk49wtc1%28v=vs.110%29.aspx
    https://msdn.microsoft.com/en-us/library/d4tt83f9(v=vs.110).aspx



    • Edited by Acamar Saturday, March 19, 2016 5:26 AM sp
    • Marked as answer by mipakteh Saturday, March 19, 2016 3:54 PM
    Saturday, March 19, 2016 5:25 AM
  • maybe my eyes are bad, but, I am not seeing a value in "reported" that is applicable to splitting. What I have always thought is that split took a really long string and split it into values separated by which ever char (I generally use comma in such string) I say throw out the array altogether... and go right from the cell to the textbox. But then there is the matter that there could be more rows than textboxs, so create the textbox in the iteration and add them to flowlayoutpanel. My new word for the day is "puzzlement" 

    Dim i As Integer = 0 For Each row As DataGridViewRow In DataGridView1.Rows If UCase(row.Cells("elem").Value) = "TS" Then Dim tb As New TextBox tb.Name = "Reportvalue" & i tb.Text = row.Cells("reported").Value 'here you can trim off the "[" "]"' FlowLayoutPanel1.Controls.Add(tb)

    i=i+1

    End If Next

    Am I way off on what he is trying to do??


    Live as if you were going to die today, learn as if you were going to live forever.


    • Edited by Gtripodi Saturday, March 19, 2016 1:54 PM
    • Marked as answer by mipakteh Saturday, March 19, 2016 3:54 PM
    Saturday, March 19, 2016 1:53 PM

All replies

  • Is there a question here?

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, March 18, 2016 3:13 PM
    Moderator
  • Hmm?

    I am not sure either, here is a shot in the dark though

    Dim MyStr As String = "val1[val2"
    Dim StrAry() As String = Split(MyStr, "[")
    
    'output'
    StrAry(0) = val1
    StrAry(1) = val2


    Live as if you were going to die today, learn as if you were going to live forever.

    Friday, March 18, 2016 3:22 PM
  • Hi Karen...

    sorry for my grammer english.

    My question is how to put the column "Reported" value to textbox.Find first put in textbox1,find second put in textbox2 .....

    Thank karen for feedback

    what i trying to do is,


    • Edited by mipakteh Saturday, March 19, 2016 4:16 AM Add
    Friday, March 18, 2016 3:48 PM
  • Thank for your split.
    Friday, March 18, 2016 3:48 PM
  • Also, not sure if it is affecting you, but you have a space in your column name "reported"

    Dim s As String = CStr(DataGridView1.Rows(i).Cells("Reported ").Value)


    Live as if you were going to die today, learn as if you were going to live forever.

    Friday, March 18, 2016 3:58 PM
  • I have Datagridview.In Column "Elem" having string "Ts".When loop through Rows then find Value of "Reported" put into TextBox.

    If your TS lines have Reported values of
      1,2,3,4
      5,6,7,8
      9,10,11,12
      13,14,15,16

    then you will get in the textboxes
      1
      6
      11
      16

    That might be what you mean.

    Dim Count as Integer = 0
    For i = 0 To 15
        If CStr(DataGridView1.Rows(i).Cells("Elem").Value) = "Ts" Then
            Dim s As String = CStr(DataGridView1.Rows(i).Cells("Reported ").Value)
            Dim parts As String() = s.Split(New Char() {CChar("["), "]"c}, StringSplitOptions.RemoveEmptyEntries)
            Select Case Count
                Case 0: 
                    TextBox9.Text = parts(count)
                Case 1: 
                    TextBox10.Text = parts(count) 
                Case 2: 
                    TextBox11.Text = parts(count) 
                Case 3: 
                    TextBox12.Text = parts(count) 
            End Select
            Count += 1
        End If
    Next


    • Edited by Acamar Friday, March 18, 2016 9:04 PM sp
    Friday, March 18, 2016 9:02 PM
  • Hi Acamar..

    I trying do it like bellow acamar.

    Thank.


    Saturday, March 19, 2016 4:14 AM
  • Hi...Gtripodi.

    Yes ..My mistake.

    Saturday, March 19, 2016 4:19 AM
  • I trying do it like bellow acamar.

    Dim Count as Integer = 0
    For i = 0 To 15
        If CStr(DataGridView1.Rows(i).Cells("Elem").Value) = "Ts" Then
            Dim s As String = CStr(DataGridView1.Rows(i).Cells("Reported ").Value)
            Dim parts As String() = s.Split(New Char() {CChar("["), "]"c}, StringSplitOptions.RemoveEmptyEntries)
    
            Select Case Count
                Case 0: 
                    TextBox9.Text = parts(1)
                Case 1: 
                    TextBox10.Text = parts(1) 
                Case 2: 
                    TextBox11.Text = parts(1) 
                Case 3: 
                    TextBox12.Text = parts(1) 
            End Select
            Count += 1
        End If
    Next
    
    
    An example like the one I posted would have avoided a lot of puzzlement.

    Saturday, March 19, 2016 4:24 AM
  • Acamar..Your code working only at Select Case 0 only then error message"Index bound outside array".

    thank .

    Saturday, March 19, 2016 4:41 AM
  • Acamar..Your code working only at Select Case 0 only then error message"Index bound outside array".

    When the error occurs, what is s?

    Saturday, March 19, 2016 4:51 AM
  • Saturday, March 19, 2016 5:02 AM
  • ... and the value of s is?

    Saturday, March 19, 2016 5:14 AM
  • 158.4
    Saturday, March 19, 2016 5:18 AM
  • 158.4

    Then the split statement is not going to give you the 3 array elements that the code expects.

    You need to add code that allows for the possibility that a field is '158.4' and not '[158.4]'.

    Or, don't use Split.  String.Replace would work, as would Trim.

    https://msdn.microsoft.com/en-us/library/fk49wtc1%28v=vs.110%29.aspx
    https://msdn.microsoft.com/en-us/library/d4tt83f9(v=vs.110).aspx



    • Edited by Acamar Saturday, March 19, 2016 5:26 AM sp
    • Marked as answer by mipakteh Saturday, March 19, 2016 3:54 PM
    Saturday, March 19, 2016 5:25 AM
  • maybe my eyes are bad, but, I am not seeing a value in "reported" that is applicable to splitting. What I have always thought is that split took a really long string and split it into values separated by which ever char (I generally use comma in such string) I say throw out the array altogether... and go right from the cell to the textbox. But then there is the matter that there could be more rows than textboxs, so create the textbox in the iteration and add them to flowlayoutpanel. My new word for the day is "puzzlement" 

    Dim i As Integer = 0 For Each row As DataGridViewRow In DataGridView1.Rows If UCase(row.Cells("elem").Value) = "TS" Then Dim tb As New TextBox tb.Name = "Reportvalue" & i tb.Text = row.Cells("reported").Value 'here you can trim off the "[" "]"' FlowLayoutPanel1.Controls.Add(tb)

    i=i+1

    End If Next

    Am I way off on what he is trying to do??


    Live as if you were going to die today, learn as if you were going to live forever.


    • Edited by Gtripodi Saturday, March 19, 2016 1:54 PM
    • Marked as answer by mipakteh Saturday, March 19, 2016 3:54 PM
    Saturday, March 19, 2016 1:53 PM
  • Not your false but my bad english.

    Acamar said my code for split cannot array 3 .it very exactly true.

    this succesed.

    Dim s As String = CStr(DataGridView1.Rows(i).Cells("Reported").Value).Trim(CChar("["), CChar("]"))
    thank gtripodi

    Saturday, March 19, 2016 3:54 PM