|
-
Sep 28th, 2000, 11:53 PM
#1
Thread Starter
Frenzied Member
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
-
Sep 28th, 2000, 11:56 PM
#2
Hyperactive Member
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.
-
Sep 29th, 2000, 12:13 AM
#3
Fanatic Member
I did some testing and found a property called SelCount.
You use it like this:
Code:
Private Sub List1_Click()
MsgBox List1.SelCount
End Sub
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.
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
-
Sep 29th, 2000, 12:21 AM
#4
Hyperactive Member
Unfortunately the person was asking about a ListVIEW not a ListBOX
They are 2 very different controls in VB.
-
Sep 29th, 2000, 12:45 AM
#5
Fanatic Member
*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:
Code:
list1.additem listview1.selecteditem
If List1.ListCount > 0 Then List1.Selected(List1.ListCount) = True
msgbox list1.SelCount
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.
I hope this helps in a better way.
-Excalibur
-
Sep 29th, 2000, 01:05 AM
#6
Lively Member
You can use this code
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
Maybe exist it a simple way by using SendMessage, but i don't know how.
-
May 6th, 2001, 03:59 PM
#7
Junior Member
LVM_GETSELECTEDCOUNT
LVM_GETSELECTEDCOUNT will do the trick
give me the handle and I`ll give you the world
Adam
junior programmer
-
May 6th, 2001, 06:56 PM
#8
Registered User
skullb,
LVM_GETSELECTEDCOUNT does not appear as a constant in WIN32API?
-
May 6th, 2001, 10:56 PM
#9
Thread Starter
Frenzied Member
Thanks skullb..
Nucleus, It's declared in commctrl.h, not windows.h.
-
May 6th, 2001, 11:07 PM
#10
Registered User
Where do I find commctrl.h?
-
May 6th, 2001, 11:29 PM
#11
Thread Starter
Frenzied Member
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|