[英]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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.