我必须分组,说明listbox1的5项并将它们转换为字符串以添加到listbox2。这是我到目前为止的代码:
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
不知何故,我可以将Listbox1中的几乎所有项组合成5个组并添加到Listbox2,但是在循环之后的listbox1中还有一些(我看是否有8个项目然后剩下3个)。你们能告诉我上面的代码我错在哪里吗?
非常感谢〜
答案 0 :(得分:1)
我认为你的算法太复杂了,下面是我接近它的方法。
希望这会有所帮助 格雷厄姆
'
' 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()