Hi,

I really need line with this I can't figure this out . I am looping through items in a list view box, I am then adding them to a string. I need each new line in the listview box to be a new line in the string. I will then add this string an access database.
But I can't get it to skip a line in the string

This is the code

For Each lvItem As ListViewItem In lstVParts.Items
                Dim i As Integer
                AllParts = AllParts & "+" & lstVParts.Items.Item(0 + i).SubItems(0).Text & " - " & lstVParts.Items.Item(0 + i).SubItems(1).Text & " - " & lstVParts.Items.Item(0 + i).SubItems(2).Text & vbCrLf
                i = i + 1
            Next

Any ideas


Thanks
James

Recommended Answers

All 3 Replies

I see vbCrLf there, so that part should be covered, really. But I see other issues with your loop logic.

You use a For Each and have a loop variable of lvItem, yet you ignore it inside the body of the loop. You declare an integer (i) variable inside the loop, use it to pull information from lstVParts.Items, and then increment it at the end of the loop body. But do you realize that the scope of i is only a single iteration within the loop body? Meaning, as soon as you iterate to the next item in the listview, the previous i is destroyed and a new i is declared, complete with a new initial value? The logic of your loop will constantly be pointing to the same item in lstVParts.Items!

You could fix that my moving the declaration of i to before the loop, but the better fix would be to get rid of i altogether. You have a loop variable of lvItem, use it!

For Each lvItem As ListViewItem In lstVParts.Items
            AllParts = AllParts & "+" & lvItem.SubItems(0).Text & " - " & lvItem.SubItems(1).Text & " - " & lvItem.SubItems(2).Text & vbCrLf
        Next

I see vbCrLf there, so that part should be covered, really. But I see other issues with your loop logic.

You use a For Each and have a loop variable of lvItem, yet you ignore it inside the body of the loop. You declare an integer (i) variable inside the loop, use it to pull information from lstVParts.Items, and then increment it at the end of the loop body. But do you realize that the scope of i is only a single iteration within the loop body? Meaning, as soon as you iterate to the next item in the listview, the previous i is destroyed and a new i is declared, complete with a new initial value? The logic of your loop will constantly be pointing to the same item in lstVParts.Items!

You could fix that my moving the declaration of i to before the loop, but the better fix would be to get rid of i altogether. You have a loop variable of lvItem, use it!

For Each lvItem As ListViewItem In lstVParts.Items
            AllParts = AllParts & "+" & lvItem.SubItems(0).Text & " - " & lvItem.SubItems(1).Text & " - " & lvItem.SubItems(2).Text & vbCrLf
        Next

Thanks for point that out.

I see vbCrLf there, so that part should be covered, really. But I see other issues with your loop logic.

You use a For Each and have a loop variable of lvItem, yet you ignore it inside the body of the loop. You declare an integer (i) variable inside the loop, use it to pull information from lstVParts.Items, and then increment it at the end of the loop body. But do you realize that the scope of i is only a single iteration within the loop body? Meaning, as soon as you iterate to the next item in the listview, the previous i is destroyed and a new i is declared, complete with a new initial value? The logic of your loop will constantly be pointing to the same item in lstVParts.Items!

You could fix that my moving the declaration of i to before the loop, but the better fix would be to get rid of i altogether. You have a loop variable of lvItem, use it!

For Each lvItem As ListViewItem In lstVParts.Items
            AllParts = AllParts & "+" & lvItem.SubItems(0).Text & " - " & lvItem.SubItems(1).Text & " - " & lvItem.SubItems(2).Text & vbCrLf
        Next

I changed that however it still does move to the next line. it just inserts a space. Anyone ideas . i think it might have something to do with being as string and not a message box or text box string.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.