devxlogo

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

Charlie has over a decade of experience in website administration and technology management. As the site admin, he oversees all technical aspects of running a high-traffic online platform, ensuring optimal performance, security, and user experience.

See also  How Engineering Leaders Spot Weak Proposals

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.