• Count of selected Items in Listbox (VB6)

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Count of selected Items in Listbox (VB6)

    Author
    Topic
    #417951

    Is there a direct Property to get to the count of selected items in a MultiSelect ListBox, or do I have to loop through their selected status like this (air code):

    Private Sub lstMyList_Click()
    Dim intC As Integer, intSel As Integer
    With lstMyList
    For intC = 1 To .ListCount
    If .Selected(intC) Then intSel = intSel + 1
    Next intC
    If .ListCount = intSel Then
    chkSelectAll = True
    Else
    chkSelectAll = False
    End If
    End With
    End Sub

    Viewing 0 reply threads
    Author
    Replies
    • #939139

      According to the MSDN documentation, the SelCount property returns the number of selected items in a ListBox control.

      • #939144

        Sorry, but I mistitled the “Version” section of my post, this is for VBA, not VB6, and .SelCount doesn’t appear to be a Property in the VBA Forms 2 Library. And my stupid loop code isn’t working either. (BTW, it’s for this albatross around my neck, still working on UI cosmetics and bugs.)

        • #939147

          You’re out of luck then – SelCount is a property of the VB6 list box, not of the MSForms list box. Looping through the list items and checking their Selected property is the only way for those…

          Apart from that:
          – Use the lstMyList_Change event instead of the lstMyList_Click event.
          – The item index is zero-based, so the loop should go For intC = 0 To .ListCount – 1

          • #939149

            Thank you Hans, that fixed everything in one shot. I then found that the corrected code overrode the “Select all items” checkbox, but I used the technique you described here to manage the problem. You are like the valiant tailor in the Grimm Brothers fairy story “seven at one stroke”. bow

    Viewing 0 reply threads
    Reply To: Count of selected Items in Listbox (VB6)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: