我有两个列表框,正在尝试将列表 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
我怎样才能让它正常工作?
最佳答案
据我所知,一旦您删除一个项目,所有项目都将变为未选中状态,因此:
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 并且您正在处理值列表,因此替换行源文本也将起作用。
关于vba - 删除列表框中的多个选定项目 - 代码仅识别第一个选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14740649/