首页 文章

如何将多个选定的列表框项目添加到另一个列表框中

提问于
浏览
0

在我的ASP.NET应用程序中,我有两个列表框,比如Listbox1和Listbox2 . 列表框1具有一些列表项,并且处于多选模式 . 如果我按下传输按钮,则列表框1中的所选项目应移至列表框2 . 我已经尝试过Single Selection Move,它运行正常 . 现在我需要多选的帮助 .

单选的代码

strItemText = lstAvailableItems.SelectedItem.Text
     iItemCode = lstAvailableItems.SelectedValue
     lstAvailableItems.Items.Remove(New ListItem(strItemText, iItemCode))
     lstSelectedItems.Items.Add(New ListItem(strItemText, iItemCode))

列表框图像

ListBox

如果我按下>按钮,单个选定项目将从“可用项目”列表框移动到所选项目列表框 . 如何进行多项选择?

2 回答

  • 0

    我的原创和@Arman的组合 .

    Dim lstRemoveItem As New List(Of ListItem)
    
        For Each li As ListItem In lstAvailableItems.Items
            If li.Selected Then
                lstRemoveItem.Add(New ListItem(li.Text, li.Value))    
                ' can't remove from the collection while looping through it       
            End If
        Next
    
        For Each li As ListItem In lstRemoveItem
            lstSelectedItems.Items.Add(li)       ' add to "selected" items
            lstAvailableItems.Items.Remove(li)   ' remove from the original available items
        Next
    
  • 3

    使用列表 . 迭代列表框中的所有项目以及找到的任何项目,将其添加到列表中 .

    Dim lstRemoveItem As New List(Of ListItem)
    
    For Each _item As ListItem In lstAvailableItems.Items
        If _item.Selected Then
            lstRemoveItem.Add(_item)
            'here, method to add item to the first list.
        End If
    Next
    
    For Each _item As ListItem In lstRemoveItem
        lstSelectedItems.Items.Add(_item)
    Next
    

    不完全是您想要的,但您可以轻松配置变量 .

相关问题