Results 1 to 11 of 11

Thread: count number of selected items?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091
    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

  2. #2
    Hyperactive Member
    Join Date
    Mar 2000
    Posts
    461
    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.

  3. #3
    Fanatic Member ExcalibursZone's Avatar
    Join Date
    Feb 2000
    Location
    Western NY State
    Posts
    908
    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

  4. #4
    Hyperactive Member
    Join Date
    Mar 2000
    Posts
    461
    Unfortunately the person was asking about a ListVIEW not a ListBOX

    They are 2 very different controls in VB.

  5. #5
    Fanatic Member ExcalibursZone's Avatar
    Join Date
    Feb 2000
    Location
    Western NY State
    Posts
    908
    *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

  6. #6
    Lively Member
    Join Date
    Jun 2000
    Location
    Belgium
    Posts
    77
    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.
    KWell

  7. #7
    Junior Member
    Join Date
    Jun 2000
    Posts
    25

    LVM_GETSELECTEDCOUNT

    LVM_GETSELECTEDCOUNT will do the trick
    give me the handle and I`ll give you the world
    Adam
    junior programmer

  8. #8
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    skullb,

    LVM_GETSELECTEDCOUNT does not appear as a constant in WIN32API?

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091
    Thanks skullb..

    Nucleus, It's declared in commctrl.h, not windows.h.

    Visual Studio 2010

  10. #10
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    Where do I find commctrl.h?

  11. #11

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091
    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

    Visual Studio 2010

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width