Results 1 to 22 of 22

Thread: Seperating text in a textbox?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2009
    Posts
    78

    Seperating text in a textbox?

    Let's say I paste this text in textbox1

    "firstname" : john, "lastname" : smith, "country" : usa, "firstname" : billy, "lastname" : brown, "country" : canada, "firstname" : richard, "lastname" : wong, "country" : usa

    How could I split up each "firstname, lastname, and country" up? And like add them to 3 listboxes?

    I'm pretty much self taught visual basic, using a lot of forums and all, andwas wondering if this is possible. I normally google too but i really have no idea what query I would search (the term). Atm, I'm having troubles making a find replace replaceall thing, so I really can't see myself making something like find '"firstname" : ' and get the text after that till the coma.

    So if someone could direct me to somewhere, explain how it works, and/or give an example it'd be great
    Last edited by Raeki; Oct 20th, 2010 at 12:29 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Seperating text in a textbox?

    1. Call Split on the string and specify a comma as the character to split on. This will give you a String array.
    2. Create three For loops, each with a Step of 3. The first loop starts at 0, the second at 1 and the third at 2.
    3. Inside the first loop, get the element at the current index and split it on the colon to get a String array.
    4. Get the second element of the array and call Trim on it, then add it to a List of first names.
    5. Repeat steps 3 & 4 for the other two loops and Lists of last names and countries.
    6. Bind the three Lists to the three ListBoxes.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Seperating text in a textbox?

    If the format never changes you could possibly do it within a single loop.

    Code:
    Dim sTmp() As String = TextBox1.Text.Split(",")
    For i As Integer = 0 To sTmp.Length - 1 Step 3
        ListBox1.Items.Add(Trim(Mid(sTmp(i), InStr(1, sTmp(i), ":") + 1)))
        ListBox2.Items.Add(Trim(Mid(sTmp(i + 1), InStr(1, sTmp(i + 1), ":") + 1)))
        ListBox3.Items.Add(Trim(Mid(sTmp(i + 2), InStr(1, sTmp(i + 2), ":") + 1)))
    Next i
    Last edited by Edgemeal; Oct 17th, 2010 at 08:52 PM.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Dec 2009
    Posts
    78

    Re: Seperating text in a textbox?

    Ok thanks I'll try that

  5. #5
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Seperating text in a textbox?

    Quote Originally Posted by Raeki View Post
    Ok thanks I'll try that
    The above requires the Microsoft.VisualBasic ref., without it this is what I came up with so far, probably needs some tweaking.
    Code:
    Dim sTmp() As String = TextBox1.Text.Split(",")
    For i As Integer = 0 To sTmp.Length - 1 Step 3
        ListBox1.Items.Add(sTmp(i).Substring(sTmp(i).IndexOf(":") + 2, (sTmp(i).Length - 1) - (sTmp(i).IndexOf(":") + 1)))
        ListBox2.Items.Add(sTmp(i + 1).Substring(sTmp(i + 1).IndexOf(":") + 2, (sTmp(i + 1).Length - 1) - (sTmp(i + 1).IndexOf(":") + 1)))
        ListBox3.Items.Add(sTmp(i + 2).Substring(sTmp(i + 2).IndexOf(":") + 2, (sTmp(i + 2).Length - 1) - (sTmp(i + 2).IndexOf(":") + 1)))
    Next i
    Last edited by Edgemeal; Oct 17th, 2010 at 08:53 PM.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Seperating text in a textbox?

    One problem with populating each ListBox separately, however you do it, is that there will be no specific association between the items in the three controls. If it's your intention that selecting an item in one list would select the corresponding items in the other two then you'd have to implement that manually.

    A better way would be to define a class that encapsulates a set of data, i.e. has properties for each of the values. You can then create a single list of instances of that type and bind it to all three controls. The association between items will then be maintained automatically.
    vb.net Code:
    1. Friend Class Record
    2.     Public Property FirstName As String
    3.     Public Property LastName As String
    4.     Public Property Country As String
    5. End Class
    vb.net Code:
    1. Dim parts As String() = myString.Split(","c)
    2. Dim values As String() = Array.ConvertAll(parts, Function(s) s.Split(":"c)(1).Trim())
    3. Dim records As New List(Of Record)
    4.  
    5. For index = 0 To values.GetUpperBound(0) Step 3
    6.     records.Add(New Record With {.FirstName = values(index),
    7.                                  .LastName = values(index + 1),
    8.                                  .Country = values(index + 2)})
    9. Next
    10.  
    11. Me.firstNameListBox.DisplayMember = "FirstName"
    12. Me.firstNameListBox.DataSource = records
    13.  
    14. Me.lastNameListBox.DisplayMember = "LastName"
    15. Me.lastNameListBox.DataSource = records
    16.  
    17. Me.countryListBox.DisplayMember = "Country"
    18. Me.countryListBox.DataSource = records
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Dec 2009
    Posts
    78

    Re: Seperating text in a textbox?

    Ok I'll try that now - yah was having troubles trying to get a way to link them together. ty again

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Dec 2009
    Posts
    78

    Re: Seperating text in a textbox?

    Hmmm where do I insert that code? Whether I post the friend class inside or ouside "public class form 1" I get errors (shown in attachement)

    sorry! i really don't know anything lol
    Attached Images Attached Images  

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Seperating text in a textbox?

    This is why you should use the Prefix drop-down to specify your version when creating a new thread. If you don't I will assume that you're using the most recent version and post code accordingly. Those are auto-implemented properties and are only supported in VB 2010. If you're using an older version then you will need to declare conventional properties with backing fields.

    Also, that is a class like any other class. You wouldn't declare it inside the form, which is itself a class. Just as you would do for any other class, you add a new code file. Use the Solution Explorer or Project menu to add a new class just as you would to add a new form, then add the appropriate properties.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Dec 2009
    Posts
    78

    Re: Seperating text in a textbox?

    Oh okay sorry I didn't know. I'm using Visual Basic 2008 but I'll fiddle around with it (i have 2010 too)

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Dec 2009
    Posts
    78

    Re: Seperating text in a textbox?

    Still have an error starting a brand new class (not sure how to explain posted in pics)

    vb Code:
    1. Public Property FirstName() As String
    2.         Get
    3.  
    4.         End Get
    5.         Set(ByVal value As String)
    6.  
    7.         End Set
    8.     End Property
    9.     Public Property LastName() As String
    10.         Get
    11.  
    12.         End Get
    13.         Set(ByVal value As String)
    14.  
    15.         End Set
    16.     End Property
    17.     Public Property Country() As String
    18.         Get
    19.  
    20.         End Get
    21.         Set(ByVal value As String)
    22.  
    23.         End Set
    24.     End Property
    Managed to get here - do I have to do anything with this?
    Attached Images Attached Images  
    Last edited by Raeki; Oct 20th, 2010 at 12:36 AM.

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Seperating text in a textbox?

    The location of the class was not the point of my post.
    Those are auto-implemented properties and are only supported in VB 2010. If you're using an older version then you will need to declare conventional properties with backing fields.
    Any beginners tutorial will cover declaring properties, e.g.

    http://www.homeandlearn.co.uk/net/nets11p5.html
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Dec 2009
    Posts
    78

    Re: Seperating text in a textbox?

    Hmmm I'm having some troubles. Whatever I put in the "get" seems to be whatever populates the listboxes

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Dec 2009
    Posts
    78

    Re: Seperating text in a textbox?

    Btw, I've been able to make it into just

    john,1234567890,canada,bc,male,mark,2345678901,usa,washington,male etc.etc.,


    I'll try to get the coding for it but if someone could give an example it'd be great

    I got this code

    vb Code:
    1. Dim myarray() As String = TextBox1.Text.Split(",")
    2.         For a As Integer = 0 To myarray.Length - 1 Step 6
    3.             ListBox1.Items.Add(myarray(a).Substring(a).IndexOf(","))
    4.             ListBox2.Items.Add(myarray(a + 1).Substring(a + 1).IndexOf(","))
    5.             ListBox3.Items.Add(myarray(a + 2).Substring(a + 2).IndexOf(","))
    6.             ListBox4.Items.Add(myarray(a + 3).Substring(a + 3).IndexOf(","))
    7.             ListBox5.Items.Add(myarray(a + 4).Substring(a + 4).IndexOf(","))
    8.             ListBox6.Items.Add(myarray(a + 5).Substring(a + 5).IndexOf(","))
    9.         Next

    And it keeps telling me "start index is greater than the length of the string"?

    And with this code, I get "start index less than 0"
    vb Code:
    1. For i As Integer = 0 To sTmp.Length - 1 Step 6
    2.             ListBox1.Items.Add(sTmp(i).Substring(sTmp(i).IndexOf(","), (sTmp(i).Length - 1) - (sTmp(i).IndexOf(",") + 1)))
    3.             ListBox2.Items.Add(sTmp(i + 1).Substring(sTmp(i + 1).IndexOf(","), (sTmp(i + 1).Length - 1) - (sTmp(i + 1).IndexOf(",") + 1)))
    4.             ListBox3.Items.Add(sTmp(i + 2).Substring(sTmp(i + 2).IndexOf(","), (sTmp(i + 2).Length - 1) - (sTmp(i + 2).IndexOf(",") + 1)))
    5.             ListBox3.Items.Add(sTmp(i + 3).Substring(sTmp(i + 3).IndexOf(","), (sTmp(i + 3).Length - 1) - (sTmp(i + 3).IndexOf(",") + 1)))
    6.             ListBox3.Items.Add(sTmp(i + 4).Substring(sTmp(i + 4).IndexOf(","), (sTmp(i + 4).Length - 1) - (sTmp(i + 4).IndexOf(",") + 1)))
    7.             ListBox3.Items.Add(sTmp(i + 5).Substring(sTmp(i + 5).IndexOf(","), (sTmp(i + 5).Length - 1) - (sTmp(i + 5).IndexOf(",") + 1)))
    8.         Next i
    Last edited by Raeki; Oct 20th, 2010 at 03:05 AM.

  15. #15
    Fanatic Member
    Join Date
    Aug 2010
    Posts
    624

    Re: Seperating text in a textbox?

    With your properties problem, here's what your new class should look like:

    Code:
    Friend Class PeopleClass
    Private xFirst as String = ""
    Private xLast as String = ""
    Private xCountry As String = ""
         Public Property FirstName() As String
              Get
                   Return xFirst
              End Get
              Set (ByVal value As String)
                   xFirst = value
              End Set
         End Property
    
         Public Property LastName() As String
              Get
                   Return xLast
              End Get
              Set (ByVal value As String)
                   xLast = value
              End Set
         End Property
    
         Public Property Country() As String
              Get
                   Return xCountry
              End Get
              Set (ByVal value As String)
                   xCountry = value
              End Set
         End Property
    End Class
    Now that you have that class, you can define a list(Of Class), like so:

    Code:
    Private ListPeople As New List(Of PeopleClass)
    Then add to the class like this (probably not the best way)

    Code:
    Dim firstArr() As String = TextBox1.Text.Split(","c)
    For i As Integer = 0 to firstArr.Count -1 Step 3
          Dim nClass As New PeopleClass
          With nClass
                .FirstName = firstArr(i).Split(":"c)(1).Trim
                .LastName = firstArr(i+1).Split(":"c)(1).Trim
                .Country = firstArr(i+2).Split(":"c)(1).Trim
           End With
           ListPeople.Add(nClass)
    Next
    And you can now simply assign the list as a datasource to a listbox and display a specific property or whatever.
    If I helped you out, please take the time to rate me

  16. #16

    Thread Starter
    Lively Member
    Join Date
    Dec 2009
    Posts
    78

    Re: Seperating text in a textbox?

    Quote Originally Posted by J-Deezy View Post
    With your properties problem, here's what your new class should look like:

    Code:
    Friend Class PeopleClass
    Private xFirst as String = ""
    Private xLast as String = ""
    Private xCountry As String = ""
         Public Property FirstName() As String
              Get
                   Return xFirst
              End Get
              Set (ByVal value As String)
                   xFirst = value
              End Set
         End Property
    
         Public Property LastName() As String
              Get
                   Return xLast
              End Get
              Set (ByVal value As String)
                   xLast = value
              End Set
         End Property
    
         Public Property Country() As String
              Get
                   Return xCountry
              End Get
              Set (ByVal value As String)
                   xCountry = value
              End Set
         End Property
    End Class
    Now that you have that class, you can define a list(Of Class), like so:

    Code:
    Private ListPeople As New List(Of PeopleClass)
    Then add to the class like this (probably not the best way)

    Code:
    Dim firstArr() As String = TextBox1.Text.Split(","c)
    For i As Integer = 0 to firstArr.Count -1 Step 3
          Dim nClass As New PeopleClass
          With nClass
                .FirstName = firstArr(i).Split(":"c)(1).Trim
                .LastName = firstArr(i+1).Split(":"c)(1).Trim
                .Country = firstArr(i+2).Split(":"c)(1).Trim
           End With
           ListPeople.Add(nClass)
    Next
    And you can now simply assign the list as a datasource to a listbox and display a specific property or whatever.
    Thanks The class code worked, and makes sense, but with the listboxing code it seems to split up the name and put it in each listbox, and I don't know what happens to the rest of the info. Trying something else right now though

    btw what is the c for in (":"c)? i don't seem to find anything with it
    Last edited by Raeki; Oct 20th, 2010 at 11:17 AM.

  17. #17

    Thread Starter
    Lively Member
    Join Date
    Dec 2009
    Posts
    78

    Re: Seperating text in a textbox?

    Lol, finally made a code, which was like half as long as the others.

    Now I just have one problem; I have inserted them seperatly into listboxes first. Can I bind them after I've inserted them without relation into the listboxes? I found some examples but they were for older versions (i'm 2008)

  18. #18
    Fanatic Member
    Join Date
    Aug 2010
    Posts
    624

    Re: Seperating text in a textbox?

    Quote Originally Posted by Raeki View Post
    Thanks The class code worked, and makes sense, but with the listboxing code it seems to split up the name and put it in each listbox, and I don't know what happens to the rest of the info. Trying something else right now though

    btw what is the c for in (":"c)? i don't seem to find anything with it
    The c in (":"c) is to implicitly declare ":" as a char. Try turning on Option Strict and then using only (":"), then try with (":"c)

    As for datasourcing the code, why not do this:

    Code:
    lstFirstNames.DisplayMember = "FirstName"
    lstFirstNames.DataSource = ListPeople
    lstLastNames.DisplayMember = "LastName"
    lstLastNames.DataSource = ListPeople
    lstCountry.DisplayMember = "Country"
    lstCountry.DataSource = ListPeople
    ?
    If I helped you out, please take the time to rate me

  19. #19
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Seperating text in a textbox?

    Quote Originally Posted by J-Deezy View Post
    The c in (":"c) is to implicitly declare ":" as a char. Try turning on Option Strict and then using only (":"), then try with (":"c)
    That would be "explicitly".
    Quote Originally Posted by J-Deezy View Post
    As for datasourcing the code, why not do this:

    Code:
    lstFirstNames.DisplayMember = "FirstName"
    lstFirstNames.DataSource = ListPeople
    lstLastNames.DisplayMember = "LastName"
    lstLastNames.DataSource = ListPeople
    lstCountry.DisplayMember = "Country"
    lstCountry.DataSource = ListPeople
    ?
    Exactly as I said in post #6.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  20. #20

    Thread Starter
    Lively Member
    Join Date
    Dec 2009
    Posts
    78

    Re: Seperating text in a textbox?

    I tried that earlier today and got a ton of extra spaces; although it may have been a mistake so I'll try again

  21. #21

    Thread Starter
    Lively Member
    Join Date
    Dec 2009
    Posts
    78

    Re: Seperating text in a textbox?

    For some strange reason, with that code it only loads like 1/6th of the items there are. Btw, I'm testing with datagrids. Is there any way to simply add the listbox items into the datagrid columns? I did some google they all seemed to require some databinding but I was wondering if there's any way to add items as you would to a listbox

  22. #22
    Fanatic Member
    Join Date
    Aug 2010
    Posts
    624

    Re: Seperating text in a textbox?

    Quote Originally Posted by jmcilhinney View Post
    That would be "explicitly".Exactly as I said in post #6.
    Ahh thanks for the correction, I always confuse myself what's implicit and what's explicit

    I know you said it in post 6, but I just thought I'd reiterate as it didn't seem like he'd done that.

    EDIT, created an app to test my code and see what could be your problem...except I didn't get any problems. Here's my FULL code. (I used a button to split the textbox and add to the list(of class), then assign datasources. Here you go:

    vb Code:
    1. Public Class Form1
    2.     Private ListPeople As New List(Of PeopleClass)
    3.  
    4.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    5.         Dim firstArr() As String = TextBox1.Text.Split(","c)
    6.         For i As Integer = 0 To firstArr.Count - 1 Step 3
    7.             Dim nClass As New PeopleClass
    8.             With nClass
    9.                 .FirstName = firstArr(i).Split(":"c)(1).Trim
    10.                 .LastName = firstArr(i + 1).Split(":"c)(1).Trim
    11.                 .Country = firstArr(i + 2).Split(":"c)(1).Trim
    12.             End With
    13.             ListPeople.Add(nClass)
    14.         Next
    15.         ListBox1.DisplayMember = "FirstName"
    16.         ListBox1.DataSource = ListPeople
    17.         ListBox2.DisplayMember = "LastName"
    18.         ListBox2.DataSource = ListPeople
    19.         ListBox3.DisplayMember = "Country"
    20.         ListBox3.DataSource = ListPeople
    21.     End Sub
    22. End Class
    23.  
    24. Friend Class PeopleClass
    25.     Private xFirst As String = ""
    26.     Private xLast As String = ""
    27.     Private xCountry As String = ""
    28.     Public Property FirstName() As String
    29.         Get
    30.             Return xFirst
    31.         End Get
    32.         Set(ByVal value As String)
    33.             xFirst = value
    34.         End Set
    35.     End Property
    36.  
    37.     Public Property LastName() As String
    38.         Get
    39.             Return xLast
    40.         End Get
    41.         Set(ByVal value As String)
    42.             xLast = value
    43.         End Set
    44.     End Property
    45.  
    46.     Public Property Country() As String
    47.         Get
    48.             Return xCountry
    49.         End Get
    50.         Set(ByVal value As String)
    51.             xCountry = value
    52.         End Set
    53.     End Property
    54. End Class
    Last edited by J-Deezy; Oct 21st, 2010 at 03:36 AM.
    If I helped you out, please take the time to rate me

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width