none
convert textbox text to array

    Question

  • Hi, I have text in a textbox and I want to convert it to an array.  If I declare the variable as an array, I get an error saying you can't do that.  If I don't declare it as an array, I get an outside range exception.  Is there any way around this limitation?

    Sunday, August 14, 2011 4:31 AM

Answers

  • That indicates that the text in textbox1 is not long enough - there is no character at position 1 or position 3.  Make the text long enought to include all 4 characters and the indexing error will go away.  You should test the text length before attempting to access parts fo the string.

    • Marked as answer by Jetramantozz Sunday, August 14, 2011 3:18 PM
    Sunday, August 14, 2011 6:27 AM

All replies

  • does your text values have delimiter and you want the value to be splited up in an array or you want your text to be one of the elements of the array

    --------------------------------------------------------

    Surender Singh Bhadauria

     

    Sunday, August 14, 2011 4:48 AM
  • i want the text to be split up into an array
    Sunday, August 14, 2011 4:59 AM
  • What sort of array to you want to convert the text to?

    You could convert it to an array of String, where each array element is a word, sentence or paragraph.

    You could convert it to an array of Char, where each array element is one letter of the text.

    Or perhaps your text is formatted data, and you want to convert it to an array of Integer or Single.

    Provide an example of the text in the text box, and a list of the array elements that the text would create.

    Sunday, August 14, 2011 4:59 AM
  • Split on what basis is there a delimiter like ',' or a ';' or any other delimiter between the values

    --------------------------------------------------------

    Surender Singh Bhadauria

     

    Sunday, August 14, 2011 5:02 AM
  •  

    x = 50

    y = 75

    varC

    textbox1.text = x & " " & y & " " & "NULL"

    I want an array to hold each character individually, so 5, 0, " ", 7, 5, " ", N, U, L, L.

     

     

    Sunday, August 14, 2011 5:03 AM
  • I don't have a delimiter no.  I just want each individual entity in the textbox (every character) to have it's own area in an array.  Acamar, your suggestion sounds most workable for me, although i'm not sure how to convert the textbox text to an array.
    Sunday, August 14, 2011 5:04 AM
  • If the array is an array of characters, then use code like this:
            Dim ara() As Char = TextBox1.Text.ToCharArray

    If the array is an array of string, use code like this:
            Dim S As String = TextBox1.Text
            
    Dim ara(S.Length - 1) As String
            For I As Integer = 0 To S.Length - 1
                ara(I) = S.Substring(I, 1)
            
    Next


    Sunday, August 14, 2011 5:11 AM
  • Try this

    Dim s As String = "asdfghjkl"
    Dim x As Char() = s.ToCharArray()


     

     


    --------------------------------------------------------

    Surender Singh Bhadauria

     


    Sunday, August 14, 2011 5:13 AM
  • just use the ToCharArray method of the TextBox.Text

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim x As Integer = 50
        Dim y As Integer = 75
    
        TextBox1.Text = x.ToString & " " & y.ToString & " " & "NULL"
    
        For Each c As Char In TextBox1.Text.ToCharArray
          Debug.WriteLine(c)
        Next
      End Sub
    

    Sunday, August 14, 2011 5:14 AM
  • ok, i used tochararray method: However, If i do this varC(20) I get an error: value of type 1-dimensional array of char cannot be converted to 1 dimensional array of object because char is not a reference type.  If i leave the (20) out, i get a runtime error of outside range exception.  Here is the code for what i'm trying to do.

     

    Private

     

    Sub

    msg()

     

    Dim

    varC

     

    If Me.InvokeRequired

    Then

     

    Me.Invoke(New MethodInvoker(AddressOf

    msg))

     

    Else

    TextBox1b.Text = readData

     

    'varC = TextBox1b.Text

     

    'varC = readData

     

    'TextBox2.Text = varC

    varC = TextBox1b.Text

     

    'GoTo 2

     

    If varC(2) = " " Then

    newx = varC(0) & varC(1)

     

    If varC(2) <> " " Then

    newx = varC(0) & varC(1) & varC(2)

     

     

    If varC(2) = " " And varC(5) = " " Then

    newy = varC(3) & varC(4)

     

    If varC(2) = " " And varC(6) = " " Then

    newy = varC(3) & varC(4) & varC(5)

     

     

    If varC(3) = " " And varC(6) = " " Then

    newy = varC(4) & varC(5)

     

    If varC(3) = " " And varC(7) = " " Then

    newy = varC(4) & varC(5) & varC(6)

     

    Convert

    .ToInt32(newx)

     

    Convert

    .ToInt32(newy)

     

    x = newx

    y = newy

     

    'TextBox2.Text = x & " : " & "y"

    PictureBox3.Location =

    New Point

    (x, y)

     

    End

    If

    2:

     

    End

    Sub

     

     

     

    Sunday, August 14, 2011 5:26 AM
  • Your code

      Dim varC

    creates an object. It is not possible to copy an array of char to an object, as the message says. I suspect you don't really want an object for varC, but it's difficult to tell from your code what you do want instead.

    Why not use the code that was posted:

    Dim varC() As Char = TextBox1.Text.ToCharArray

    Or just use the array indexing that is autoamtically available with a string

    Dim varC as String = TextBox1.Text
    If varC(1) = " " AndAlso varC(3) = "2" Then ...



    Sunday, August 14, 2011 6:00 AM
  • Your code is very difficult to read and I can't see where you have declared X and Y, but since newX and newY appear to be strings, then I assume X and Y are also strings, and there is no constructor for a point that takes two strings.   You need to convert the result to integer and pass two integers to the point constructor.  Something like

    PictureBox3.Location = NewPoint(Convert.ToInt32(NewX), Convert.ToInt32(newY))

    Sunday, August 14, 2011 6:18 AM
  • hmm, Acamar when I do this:

    Dim varC as String = TextBox1.Text
    If varC(1) = " " AndAlso varC(3) = "2" Then ...



    I still get an index outside bounds of array error on the IF THEN code.

    Sunday, August 14, 2011 6:18 AM
  • That indicates that the text in textbox1 is not long enough - there is no character at position 1 or position 3.  Make the text long enought to include all 4 characters and the indexing error will go away.  You should test the text length before attempting to access parts fo the string.

    • Marked as answer by Jetramantozz Sunday, August 14, 2011 3:18 PM
    Sunday, August 14, 2011 6:27 AM
  • hmm, the text in the textbox IS long enough.  I think the problem stems from another part of the program.  I'm not sure it's good to start the question on this thread, i'll start a new thread.  Thanks for all your help.
    Sunday, August 14, 2011 3:18 PM