【问题标题】:removing multiple selected items in a listbox - code only recognising first selection删除列表框中的多个选定项目 - 仅识别第一个选择的代码
【发布时间】:2013-01-22 08:01:58
【问题描述】:

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

我的添加项工作正常:

'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 中删除项目时,它只会将第一个选定的项目识别为已选中,并跳过我选择的其他项目。这就是问题。这是我的代码:

'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

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

【问题讨论】:

  • 您使用的是哪个 MS Office 应用程序?
  • Access 2010 - 抱歉,我应该在描述中包含这个!

标签: vba ms-access listbox multi-select


【解决方案1】:

据我所知,一旦您删除一个项目,所有项目都会被取消选中,所以:

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 并且您正在处理一个值列表,因此在 Row Source 的文本上替换也可以工作。

【讨论】:

  • 谢谢!抱歉,如果这是一个愚蠢的问题,但我应该将每个变量声明为什么?我假设它是对象,srem 和 asrem 是字符串,但后来我收到一个错误,突出显示 UBound 并期待一个数组。
  • ...现在我在遇到第一个 for 循环时出现运行时错误。我觉得我错过了一些非常明显的东西。
  • 我所做的唯一更改是声明每个变量,然后将“lst2”更改为表单中列表框的实际名称。其他一切都保持不变。我不确定你所说的“事件”是什么意思,我还在学习中!
  • 啊,是的,我当然有,代码在单击“删除”按钮时执行,但随后会引发运行时错误,调试器告诉我在遇到第一个 For 循环时会发生该错误。跨度>
【解决方案2】:

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

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

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

【讨论】:

  • 我喜欢这个。循环遍历集合(尤其是删除)时,您应该倒退。
  • 删除多个项目时,您必须向后退以正确执行该操作 :) 例如考虑像 ["Jim", "Steve", "Bob", " Mary"] For i = 1 to 4 lst(i).Delete Next 这将在 i = 1 时起作用,Jim 将被删除。但它会调整列表的大小,现在当 i = 2 时将删除 Bob。如果您继续前进,列表中的原始第二项 Steve 将永远不会被删除。一旦你达到 i = 3,你会得到一个错误,因为此时列表只包含 2 个剩余项目(史蒂夫,玛丽)
  • 我投票支持你倒退,但不幸的是,我认为它不会在这种情况下起作用。
  • 我看到了你的答案——如果 RemoveItem 取消选择所有其他选定的项目,那么你是对的,这种方法将不起作用(在其他应用程序中 RemoveItem 不会取消选择其他项目) .
  • @DavidZemens 我在 Excel 中尝试了您的代码,它运行良好。 RemoveItem 不会取消选择 excel 中的任何其他项目
【解决方案3】:

与@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

【讨论】:

    【解决方案4】:

    我用过这段代码,效果很好

    Dim ArraySlctd() As Variant  'this Arry is to save what row is selected because when remove any row all selected are getting false
    ReDim ArraySlctd(0 To Me.List1.ListCount - 1)
    For lp = 0 To Me.List1.ListCount - 1 '
    ArraySlctd(lp) = Me.List1.Selected(lp) 'work in the same range as the selected property
    Next lp 
    

    现在使用起来很简单

    For lp = 0 To Me.List1.ListCount - 1
    If ArraySlctd(lp) = True Then
    'Remove Or Change(by remove and AddIten with the same Index)
    End If
    
    Next lp
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-26
      • 1970-01-01
      • 1970-01-01
      • 2011-10-27
      相关资源
      最近更新 更多