Link to home
Start Free TrialLog in
Avatar of lee88
lee88Flag for United States of America

asked on

detect which (multiple) items are selected in listview ?

I have a listview, lvwInclude, that has a bunch of line items in it. MultiSelect property is enabled.
I click on one item, then hold shift and click another to select multiple line items.

But, when I execute this loop (by clicking on a cammand button), the result is that the nSelectedCount count is only one, which indicates that only one item is selected?

'************************************
Private Sub Command1_Click()
    Dim nSelectedCount As Integer
    Dim lItem As ListItem
   
    'See if multiple listview items are selected:
    nSelectedCount = 0
    For Each lItem In lvwInclude.ListItems
        If lItem.Selected Then
            nSelectedCount = nSelectedCount + 1
        End If
    Next
    Debug.Print nSelectedCount

End Sub
'***********************************

What do I have to do to count how many line items are multi-selected?
ASKER CERTIFIED SOLUTION
Avatar of Dana Seaman
Dana Seaman
Flag of Brazil image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lee88

ASKER

My code does not work in my app. Could it be a property setting? see attached screen shot
proppg.jpg
Avatar of Leithauser
Leithauser

Use this code:

Dim nSelectedCount As Integer, X As Integer
nSelectedCount = 0
For X = 0 To lvwInclude.ListCount - 1
     If lvwInclude.Selected(X) Then
        nSelectedCount = nSelectedCount + 1
    End If
Next X
Label1 = Str$(nSelectedCount)
Avatar of lee88

ASKER

Leithauser: I get error: Listview does not have a ListCount property
There is no Listview in my code
Avatar of lee88

ASKER

the original question is:
What do I have to do to count how many line items are multi-selected
IN MY LISTVIEW
<<the original question is:
What do I have to do to count how many line items are multi-selected
IN MY LISTVIEW>>

Sorry, I misread the question. I thought it was a listbox.
Set HideSelection = False. Your PropertyPage shows it checked.
Avatar of lee88

ASKER

Leithauser: no prob. Is there a similar approach for a listview?

danaseaman: I have tried HideSelection both ways. I currently have it unchecked, but my nSelectedCount still comes up as 1 instead on the count of selected items. However, with HideSelection unchecked, I can see the selected items as highlighted after the ListView loses focus.

Avatar of lee88

ASKER

You were correct. Nothing wrong with the code. I had cut-and-pasted the name of a different listview by mistake. Thanks for your help.