Click here to Skip to main content
15,891,864 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello! I've been trying to figure this out for a few days now

I want to split a string and add the items into a listview column

i managed to get it working but it only adds 1 line for some reason, how do i do it until there is nothing left?

example:

1|name|address

Dim s As String = TextBox2.Text

       Dim split As String() = s.Split(New [Char]() {"|"})


       ListView1.Items.Add(split(0))
           ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(split(1))
           ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(split(2))


textbox2.text contains "1|name|address"

the code converts it into a string, the string is split
this works for ONLY the FIRST LINE
if i have multiple lines in textbox2 / string , it ignores them.

1 will be in first column, name will be in second and address will be in third column

if the string has this:
1|name|address

2|name|address


it will ignore the second line, add the first line and end at that

could you pls help? thanks

What I have tried:

I have tried everything from googling, it won't read all the lines
Posted
Updated 28-Mar-18 8:14am
Comments
Member 13356923 28-Mar-18 11:24am    
Normally if i read it direct from a file via streamreader it would be something like:

        Do While inputstream.Peek > 0
                'Split each line containing Account|Password into the array
                newstr = inputstream.ReadLine().Split("|")
                'Assigm the values to the variables

                'Add them to the list
                'ref
                My.Forms.Database.ListView1.Items.Add(newstr(0))
                'name
                My.Forms.Database.ListView1.Items.Item(My.Forms.Database.ListView1.Items.Count - 1).SubItems.Add((newstr(1)))


but i am not reading from a while, its a string which makes it abitmore harder
the "do while inputstream.peek > 0" is what i am currently missing, but how do i do this for a string?
Richard MacCutchan 28-Mar-18 11:36am    
It only adds one item because that is all you tell it to do. You need to set up a For or Do loop that deals with each set of 3 strings.

And if you have multiple lines you most likely also need to split at newline characters.

You only process the first line!
If you want to process more lines from a multiline textbox, then you need to use the TextBox.Lines property in a For Each loop, and repeat that code inside the loop for it processes each line separately.
 
Share this answer
 
Comments
Member 13356923 28-Mar-18 11:35am    
Hello i just tried

Dim s As String = TextBox1.Text

Dim split As String() = TextBox1.Text.Split(New [Char]() {"|"})

For Each l In TextBox1.Lines
ListView1.Items.Add(split(0))
ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(split(1))

but it just repeats and adds the FIRST line into the listview :(

and i don't want it to read from the textbox, i want it to read from a string
OriginalGriff 28-Mar-18 11:48am    
Oh, fer ...

Look at the code you wrote.
You need the Split to be performed on each line in the input, not on the first line in a text box, and then hope it magically does it for you on all the other ones you can find inside a loop...
Richard MacCutchan 28-Mar-18 12:04pm    
Calm down dear!
OriginalGriff 28-Mar-18 12:15pm    
The change to summer time always messes with my head... :laugh:

But I used to hate Winner - pompous, self-satisfied prat - his column in the Sunday Times was the one I ignored most studiously.
Richard MacCutchan 28-Mar-18 12:28pm    
I was thinking of Cameron, but now you mention Winner I remember those annoying insurance ads.
 
Share this answer
 
Comments
Maciej Los 28-Mar-18 13:29pm    
Short And To The Point!
Member 13356923 28-Mar-18 13:49pm    
could someone just spoon feed me and provide me a code T-T
im tearing my hair out here
In addition to soultion #1 by OriginalGriff[^] and solution #2 by Richard MacCutchan[^] ...

You should work on data instead of its string representation. It's called Object Oriented Programming[^].

Insert new class (name it: MyData) to your project and paste below code:

VB.NET
Public Class MyData
	Private iID As Integer = 0
	Private sName As String = String.Empty
	Private sAddress As String = String.Empty
	
	
	Public Sub New(ByVal sPipeLine As String)
		Dim parts As String()= sPipeLine.Split(New String(){"|"}, StringSplitOptions.RemoveEmptyEntries)
		iID = Int32.Parse(parts(0))
		sName = parts(1)
		sAddress = parts(2)
	End Sub
	
	Public Property ID As Integer
		Get
			Return iID
		End Get 
		Set (value As Integer)
			iID = value
		End Set
	End Property
	
	Public Property Name As String
		Get
			Return sName
		End Get 
		Set (value As String)
			sName = value
		End Set
	End Property

	Public Property Address As String
		Get
			Return sAddress
		End Get 
		Set (value As String)
			sAddress = value
		End Set
	End Property

	Public Overrides Function ToString() As String
		Return String.Format("{0}|{1}|[2}", iID, sName, sAddress)
	End Function

	Public Function ToArray() As String()
		Return New String(){iID.ToString(), sName, sAddress}
	End Function

End Class


How to use it?
VB.NET
    'for testing purposes i use this:
    Dim lines As String() = {"1|name|address", "2|name|address"}
    'in your case - you have to get it from TextBox.Lines

Dim data As List(Of MyData) = lines _
    .Select(Function(x) New MyData(x)) _
    .ToList()

For Each md As MyData In data
            'this creates item with subitems
    Dim lvi As ListViewItem = New ListViewItem(md.ToArray())
    Me.ListView1.Items.Add(lvi)
Next


For further details, please see:
ListViewItem Class (System.Windows.Forms)[^]
ListViewItem Constructor (String[]) (System.Windows.Forms)[^]
Object.ToString Method (System)[^]
 
Share this answer
 
Comments
Member 13356923 28-Mar-18 15:13pm    
This works perfectly thank u! just one more error i have come across

if my textbox has || (with nothing in between the split) it gives a exception
index was outside the bounds of the array

Is there a solution to this?

this also happens if there is a empty " " at the end of the textbox
Maciej Los 28-Mar-18 15:47pm    
Yes, there is. Change MyData constructor to:
	Public Sub New(ByVal sPipeLine As String)
		Dim parts As String()= sPipeLine.Split(New String(){"|"}, StringSplitOptions.RemoveEmptyEntries)
		If parts.Length = 3 Then
			iID = Int32.Parse(parts(0))
			sName = parts(1)
			sAddress = parts(2)
		End If
	End Sub

If my answer was helpful, please accept it as a solution (green button).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900