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

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS