devxlogo

ArrayToListbox – Add an array of strings to a ListBox or ComboBox

ArrayToListbox – Add an array of strings to a ListBox or ComboBox

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal _    hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _    lParam As Any) As LongPrivate Declare Function LockWindowUpdate Lib "user32" Alias "LockWindowUpdate" _    (ByVal hwndLock As Long) As LongPrivate Const LB_ADDSTRING = &H180Private Const LB_RESETCONTENT = &H184Private Const CB_ADDSTRING = &H143Private Const CB_RESETCONTENT = &H14B' Add an array of string to a listbox or combo box' using API functions, and optionally clearing it first' This procedure is faster than the one based on AddItem methods,' especially with ComboBox controls, and reduces flickering'' FIRST and LAST indicate which portion of the array' should be considered; they default to the first' and last element, respectivelySub ArrayToListBox(ctrl As Object, arr As Variant, Optional clearIt As Boolean, _    Optional ByVal First As Variant, Optional ByVal Last As Variant)    Dim msgReset As Long    Dim msgAdd As Long    Dim hWnd As Long    Dim index As Long        If TypeOf ctrl Is ListBox Then        msgReset = LB_RESETCONTENT        msgAdd = LB_ADDSTRING    ElseIf TypeOf ctrl Is ComboBox Then        msgReset = CB_RESETCONTENT        msgAdd = CB_ADDSTRING    Else        ' none of the above, exit        Exit Sub    End If        If IsMissing(First) Then First = LBound(arr)    If IsMissing(Last) Then Last = UBound(arr)        ' disable redrawing    hWnd = ctrl.hWnd    LockWindowUpdate hWnd        ' clear control if requested    If clearIt Then        SendMessage hWnd, msgReset, 0, 0    End If        ' add all items in the array    For index = First To Last        SendMessage hWnd, msgAdd, 0, ByVal CStr(arr(index))    Next    ' re-enable redrawing    LockWindowUpdate 0End Sub

See also  5 Tips for Working With an Onsite Interpreter
devxblackblue

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.

About Our Journalist