vba 删除列表框中的多个选定项目 - 代码仅识别第一个选择

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14740649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 14:54:42  来源:igfitidea点击:

removing multiple selected items in a listbox - code only recognising first selection

vbams-accesslistboxmulti-select

提问by user1844098

I have two listboxes and am trying to add items from List 1 to List 2, and then be able to remove multiple items from List 2 at once. Note that List 1 stays stagnant (this is how it is supposed to be).

我有两个列表框,正在尝试将列表 1 中的项目添加到列表 2,然后能够一次从列表 2 中删除多个项目。请注意,列表 1 保持停滞(这是应该的)。

I have the adding items working right:

我的添加项目正常工作:

'Add the selected items to List 2
Dim i As Integer

If lst1.ItemsSelected.Count > 0 Then
    i = 0
    While i < lst1.ListCount
        If lst1.Selected(i) Then
            lst2.AddItem (lst1.ItemData(i) & ";" & lst1.Column(1, i) & ";")
            lst1.Selected(i) = False
        End If
        i = i + 1
    Wend
End If

However, when I try to remove the items from List 2 in a similar way, it only recognises the first selected item as selected and skips over the other items that I have selected. This is the problem. Here is my code:

但是,当我尝试以类似的方式从列表 2 中删除项目时,它只会将第一个选定项目识别为选定项目,并跳过我选择的其他项目。这就是问题。这是我的代码:

'Remove the selected items from List 2
Dim i As Integer

If lst2.ItemsSelected.Count > 0 Then
    i = lst2.ListCount - 1
    While i >= 0
       If lst2.Selected(i) Then
           lst2.RemoveItem (i)
           lst2.Selected(i) = False
       End If
        i = i - 1
    Wend
End If

How can I get this working correctly?

我怎样才能让它正常工作?

回答by Fionnuala

As far as I can tell, as soon as you remove one item, all items become unselected, so:

据我所知,一旦您删除一个项目,所有项目都将变为未选中状态,因此:

Dim itm As Variant
Dim srem As String
Dim asrem As Variant

    For Each itm In lst2.ItemsSelected
        srem = srem & "," & itm
    Next

    asrem = Split(Mid(srem, 2), ",")
    For i = UBound(asrem) To 0 Step -1
        lst2.RemoveItem lst2.ItemData(asrem(i))
    Next

Note also that this is Access and you are dealing with a value list, so Replace on the text of Row Source will also work.

另请注意,这是 Access 并且您正在处理值列表,因此替换行源文本也将起作用。

回答by David Zemens

Try using a for/next loop instead of While?

尝试使用 for/next 循环而不是 While?

Something like this works in PPT/XLS, should be similar in Access I think.

像这样的东西在 PPT/XLS 中有效,我认为在 Access 中应该是类似的。

For i = lst2.ListCount - 1 to 0 step -1
    If lst2.Selected(i) = True Then
        lst2.RemoveItem(i)
    End If
Next

回答by Anthony Griggs

Same Concept as @Fionnuala with the exception that I used a collection which I generally find more flexible:

与@Fionnuala 相同的概念,除了我使用了一个我通常觉得更灵活的集合:

Dim i As Integer
Dim listColl As Collection
Dim Item As Variant

Set listColl = New Collection
With Me.listAvailable
    ' Add Selected Items to Collection (MultiSelect Listbox)
     For i = 0 To .ListCount - 1
        If .Selected(i) Then
            listColl.Add .ItemData(i)
        End If
    Next i
End With

For Each Item In listColl
    Me.listSelected.AddItem Item
    Me.listAvailable.RemoveItem Item
Next Item