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
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.
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:
Friend Class Record
Public Property FirstName As String
Public Property LastName As String
Public Property Country As String
End Class
vb.net Code:
Dim parts As String() = myString.Split(","c)
Dim values As String() = Array.ConvertAll(parts, Function(s) s.Split(":"c)(1).Trim())
Dim records As New List(Of Record)
For index = 0 To values.GetUpperBound(0) Step 3
records.Add(New Record With {.FirstName = values(index),
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.
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.
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
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
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)
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)
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
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:
Public Class Form1
Private ListPeople As New List(Of PeopleClass)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
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
ListBox1.DisplayMember = "FirstName"
ListBox1.DataSource = ListPeople
ListBox2.DisplayMember = "LastName"
ListBox2.DataSource = ListPeople
ListBox3.DisplayMember = "Country"
ListBox3.DataSource = ListPeople
End Sub
End Class
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
Last edited by J-Deezy; Oct 21st, 2010 at 03:36 AM.
If I helped you out, please take the time to rate me