删除列表框中的多个选定项-仅识别第一个选择的代码 [英] removing multiple selected items in a listbox - code only recognising first selection

查看:98
本文介绍了删除列表框中的多个选定项-仅识别第一个选择的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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).

我有添加项正常工作:

'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

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

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:

'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

如何使它正常工作?

推荐答案

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

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

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

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.

这篇关于删除列表框中的多个选定项-仅识别第一个选择的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆