繁体   English   中英

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

[英]How to get Multiple selected listbox items to another listbox

在我的ASP.NET应用程序中,我有两个列表框,例如Listbox1和Listbox2。 Listbox1有一些列表项,处于多重选择模式。 如果我按传输按钮,则Listbox1中的选定项目应移至Listbox2。 我已经为单选移动尝试过它,并且效果很好。 现在,我需要多选的帮助。

单选代号

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

列表框图片

列表框

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

我的原版和@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

使用清单。 遍历列表框中的所有项目,以及发现要选择的所有项目,将其添加到列表中。

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

并非完全符合您的要求,但是您可以轻松配置变量。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM