我需要ListView中已检查项目的计数。到目前为止,我尝试了这种方法:
Debug.Print ListView1.ListItems.Item(ListView1.ListItems.Count).Checked //outputs "false"
Debug.Print ListView1.ListItems(ListView1.ListItems.Count).Checked //outputs "false"
Debug.Print ListView1.ListItems.Count //outputs 13, because the LV contains 13 items
是否有可能在列表视图中返回checked
个项目的数量而没有循环列表视图。
答案 0 :(得分:1)
虽然OP想要一种非循环方法 - 这里是为了完整性。很高兴用更整洁的非循环方法投票给答案!
迭代ListItems
集合并计算Selected
属性为True
的项目:
Option Explicit
Private Sub UserForm_Initialize()
With Me.ListView1
.ListItems.Add 1, "Hughie", "Hughie"
.ListItems.Add 2, "Dewey", "Dewey"
.ListItems.Add 3, "Louis", "Louis"
.View = lvwList
.MultiSelect = True
End With
End Sub
Private Sub CommandButton1_Click()
MsgBox GetSelectedCount(Me.ListView1)
End Sub
Function GetSelectedCount(lvw As ListView) As Long
Dim lngSelectedCounter As Long
Dim lngCounter As Long
For lngCounter = 1 To lvw.ListItems.Count
If lvw.ListItems(lngCounter).Selected Then
lngSelectedCounter = lngSelectedCounter + 1
End If
Next lngCounter
GetSelectedCount = lngSelectedCounter
End Function