Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: VB6
Expertise: Intermediate
Nov 4, 2000



Building the Right Environment to Support AI, Machine Learning and Deep Learning

GetListBoxSelectRange - Retrieve the index of all the selected items in a ListBox

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal _
    hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
    lParam As Any) As Long

' Return an array with the indexes of all the selected items in a ListBox 
' control
' the array is zero-based, but the significant values 
' are from index 1 onward
' This is how you can use this routine
'   Dim i As Long
'   Dim indexes() As Long
'   indexes() = GetListBoxSelectRange(List1)
'   For i = 1 To UBound(indexes)
'       Debug.Print "Selected " & indexes(i)
'   Next

Function GetListBoxSelectRange(lb As ListBox) As Long()
    Dim selCount As Long

    ' Retrieve the number of selected items (same as SelCount property)
    selCount = SendMessage(lb.hwnd, LB_GETSELCOUNT, 0, ByVal 0&)
    ' prepare the result array
    ReDim result(0 To selCount) As Long

    ' read all the indexes in one operation
    If selCount > 0 Then 
        SendMessage lb.hwnd, LB_GETSELITEMS, selCount, result(1)
    End If
    ' return the array
    GetListBoxSelectRange = result()
End Function
Francesco Balena
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date