如何将 "x" 个列表框项分组并添加到另一个列表框?
How to group "x" listbox items and add to another listbox?
我必须对 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 个)。你们能告诉我上面的代码哪里错了吗?
非常感谢~
我觉得你的算法太复杂了,下面是我的处理方法。
希望对您有所帮助
格雷厄姆
'
' 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()
我必须对 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 个)。你们能告诉我上面的代码哪里错了吗?
非常感谢~
我觉得你的算法太复杂了,下面是我的处理方法。
希望对您有所帮助 格雷厄姆
'
' 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()