You are here ComboBox and ListBox have image - BSAC Controls

ComboBox and ListBox have image - BSAC Controls

ComboBox have image in item and ListBox have image item  - BSAC controls

In VBA, do with step by step:

Step 1: Create Userform

Make sure you have installed Add-in A-Tools or BSAC.ocx activex controls before do Step 2

Step 2: Click to userform, see window "Toolbox", right click on tab "Controls" -> "Import Page", select file "ImportToToolbox.pag" (Download)

(Do Step 2 only if you do not see BSAC controls on Toolbox window)

Step 3: Drag controls to userform: BSComboBox, BSListNox, BSButton, BSImageList

Step 4: Right click on userform -> View Code. Now you have widow to edit code. Copy below code:
'-------BEGIN COPY
'Author: Nguyen Duy Tuan - -
#If VBA7 Then
Private Declare PtrSafe Function DestroyIcon Lib "User32.dll" (ByVal HIcon As LongPtr) As Long
Private Declare Function DestroyIcon Lib "User32.dll" (ByVal HIcon As Long) As Long
#End If
'VBA programming with BSAC - Bluesofts ActiveX Controls
Private Sub UserForm_Initialize()
    #If VBA7 And Win64 Then
        Dim hIcon() As LongPtr, hSmIcon() As LongPtr
        Dim hIcon() As Long, hSmIcon() As Long
    #End If
    Dim I&, n As Long, idx&
    'Setup ImageList for display icon in Combobox
    BSImageList1.PicHeight = 32
    BSImageList1.PicWidth = 32
    BSImageList1.UseImageListDraw = True 'BSAC v2.0.0.6 - 05-04-2019
    'Link BSImageList to BSComboBox
    Set BSComboBox1.ImageList = BSImageList1
    'Setup BSCombobox display image in item
    BSComboBox1.Style = csExpertAutoBuild
    BSComboBox1.ItemHeight = 32
    'Link BSImageList to BSListBox
    Set BSListBox1.ImageList = BSImageList1
    'Setup BSListBox display image in item
    BSListBox1.Style = dsExpertAutoBuild
    BSListBox1.ItemHeight = 32
    n = ExtractIconEx(GetSysDir & "\shell32.dll", -1, 0&, 0&, 0&) 'Count number of icons in "shell32.dll"
    ReDim hIcon(n - 1), hSmIcon(n - 1) 'Resize array to keep HICON
    n = ExtractIconEx(GetSysDir & "\shell32.dll", 0, hIcon(0), hSmIcon(0), n) 'Copy n icon from "shell32.dll" to array
    For I = 0 To n - 1
        idx = BSImageList1.ListImages.AddIcon(hIcon(I)) 'Add icon to BSImageList
        BSComboBox1.Items.Add "Icon index: " & I, idx 'Add item and link icon to ComboBox
        BSListBox1.Items.Add "Icon index: " & I, idx 'Add item and link icon to ListBox
        DestroyIcon hIcon(I) 'Free memory
        DestroyIcon hSmIcon(I) 'Free memory
    Next I
End Sub
Private Sub UserForm_Terminate()
End Sub
Private Sub BSComboBox1_OnSelect()
    Label2.Caption = BSComboBox1.Selected.Text
    Label2.Enabled = True
End Sub
'-------END COPY