且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ASP.NET(VB)列表框未将所有选定项移动到其他列表框

更新时间:2023-11-28 20:32:28

我将所选项目添加到列表中;然后遍历列表进行添加和删除.当一个集合或索引在一个循环中混乱时,它可能会产生意想不到的结果.

I added the selected items to a list; then looped through the list to do the adding and removing. When a collection or indexes are messed with inside a loop it can have unexpected results.

Protected Sub Button1_Click(ByVal sender As Object, e As System.EventArgs) Handles Button1.Click
    Dim lstItems As New List(Of ListItem)
    For Each item As ListItem In ListBox1.Items
        If item.Selected Then
            lstItems.Add(item)
        End If
    Next
    'It wasn't clear if you wanted the items to remain selected after moving.
    ListBox1.ClearSelection()
    Debug.Print(lstItems.Count.ToString)
    For Each item As ListItem In lstItems
        ListBox2.Items.Add(item)
        ListBox1.Items.Remove(item)
    Next
End Sub