devxlogo

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

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 LongConst LB_GETSELITEMS = &H191' 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)'   NextFunction 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

devx-admin

Share the Post: