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?
lee88Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

danaseamanCommented:
Hold down Ctrl to select random multiple items. Use shift is to select a block of items.
Your code works fine here.
Try this sample:

Option Explicit

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

Private Sub Form_Load()
   Dim i As Long
   lvwInclude.ColumnHeaders.Add , , "Column 1"
   For i = 0 To 10
      lvwInclude.ListItems.Add , , "Item " & i
   Next
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
lee88Author Commented:
My code does not work in my app. Could it be a property setting? see attached screen shot
proppg.jpg
0
LeithauserCommented:
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)
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

lee88Author Commented:
Leithauser: I get error: Listview does not have a ListCount property
0
LeithauserCommented:
There is no Listview in my code
0
lee88Author Commented:
the original question is:
What do I have to do to count how many line items are multi-selected
IN MY LISTVIEW
0
LeithauserCommented:
<<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.
0
danaseamanCommented:
Set HideSelection = False. Your PropertyPage shows it checked.
0
lee88Author Commented:
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.

0
lee88Author Commented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.