I have to group, says 5 items of listbox1 and convert them to a string to add to listbox2. Here is the code I have so far:
dim s as string="" 'a string to collect listbox1 items
dim count as integer=Listbox1.items.count
Do While count > 0
Select Case count
Case Is <= 5
For i = 0 To ListBox1.Items.Count - 1
s &= ListBox1.Items.Item(i).ToString
ListBox1.Items.RemoveAt(i)
Next
ListBox2.Items.Add(s)
Exit Do 'If there are <=5 items, then done , exit loop
Case Is > 5
For i = 0 To 4
s &= ListBox1.Items.Item(i).ToString
ListBox1.Items.RemoveAt(i) 'delete each item in listbox1, after add
Next
ListBox2.Items.Add(s)
s = "" ' Reset the s string to receive new items
count = count - 5 'reduce count and loop over again
End Select
Loop
Somehow, I could group almost the items in Listbox1 into groups of 5 and add to Listbox2, however there are some left in the listbox1 after the Loop (I see if I have 8 items then 3 is left). Could you guys show me where I was wrong in the code above?
Thank you very much ~
I think your algorithm is too complicated, below is how i would approach it.
Hope this helps Graham
'
' Move items from ListBox1 to ListBox2
'
Dim s As String = ""
For count As Integer = 0 To ListBox1.Items.Count - 1
' update every 5
If (count Mod 5 = 0) Then
' Only update if not the first time
If (count <> 0) Then
ListBox2.Items.Add(s)
s = ""
End If
End If
s = s + ListBox1.Items.Item(count).ToString
Next
'
' Add the last ones
'
If (s <> "") Then
ListBox2.Items.Add(s)
End If
'
' Clear down listbox 1
'
ListBox1.Items.Clear()