Hi,
How can I tell how many items are selected in a listview?
ListView.SelectedItem does not seem to have a "Count" event...
Thanks,
Dan
Printable View
Hi,
How can I tell how many items are selected in a listview?
ListView.SelectedItem does not seem to have a "Count" event...
Thanks,
Dan
The "SelectedItem" attribute is basically a mapping to the specific OBJECT in the ListItems collection based on the most recently selected Item (ie the one with the setfocus around).
To actually count them, unfortunately there is no other way but to iterate through the ListItems collection and increment a counter for each item where "Selected" is true.
I know this is a pain but unfortunately it is a limitation of the control.
I did some testing and found a property called SelCount.
You use it like this:
It will return the number of items that are currently selected within the listbox, whether it's style is a checkbox, or normal, and whether it's Multi-Select is set at None, Simple, or Extended.Code:Private Sub List1_Click()
MsgBox List1.SelCount
End Sub
This doesn't, however, tell you exactly which items are selected, only that X number of the items are selected. To do that, I'd suggest setting up an Array that keeps track of the items. You can cycle through the list with a for loop as well.
Hope this helps you out :)
-Excalibur
Unfortunately the person was asking about a ListVIEW not a ListBOX
They are 2 very different controls in VB.
*doh* My mistake.
My suggestion then? each time an item is clicked on and it's selected state changes, add or subtract 1 (or the number of items) to the ListView1.Tag property. I haven't worked too much with ListViews (I've been working with formless code more often than not).
Another, more simple idea would be to add the selected items to a hidden listbox and check it's SelCount property. Make sure you have the multi-select to true and set each item to selected = true as you add them, otherwise the SelCount would be off. All you have to do is add the selecteditem from the listview to the listbox:
It *may* or *may not* work, you'll probably have to assign a key to each item (say it's index in the listview) to remove it should the item be de-selected.Code:list1.additem listview1.selecteditem
If List1.ListCount > 0 Then List1.Selected(List1.ListCount) = True
msgbox list1.SelCount
I hope this helps in a better way.
-Excalibur
You can use this code
Maybe exist it a simple way by using SendMessage, but i don't know how.Code:Function CountSelectItem(lv As ListView) As Integer
Dim Count As Integer
Dim i As Integer
Count = 0
For i = 1 To lv.ListItems.Count
If lv.ListItems(i).Selected Then
Count = Count + 1
End If
Next i
CountSelectItem = Count
End Function
LVM_GETSELECTEDCOUNT will do the trick
skullb,
LVM_GETSELECTEDCOUNT does not appear as a constant in WIN32API?
Thanks skullb..
Nucleus, It's declared in commctrl.h, not windows.h.
Where do I find commctrl.h?
If you have VC++ installed, it should be in the VC98\Include directory. But I'm assuming that you don't because you're asking. If you're asking where you can find it, I'm sure you can download it from Microsoft somewhere, but I'm not sure where.. Anybody?
If you need it, I can email it to you.. Just let me know..
Dan