5

我有两个列表框,正在尝试将列表 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

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

4

4 回答 4

8

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

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 的文本进行替换也将起作用。

于 2013-02-07T12:42:51.407 回答
7

尝试使用 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
于 2013-02-07T07:08:51.303 回答
0

与@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
于 2017-08-24T18:43:04.367 回答
0

我使用了这段代码,它工作正常

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
于 2020-07-09T22:23:57.337 回答