You are here ComboBox and ListBox have image - BSAC Controls

ComboBox and ListBox have image - BSAC Controls

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

(Download Source Code)

In VBA, do it step by step:

Step 1: Create a Userform

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

Step 2: Click on the Userform, view the "Toolbox"  window, right click on the "Controls" tab => "Import Page", select the "ImportToToolbox.pag" file (Download) 

(Only do Step 2 if you do not see the BSAC controls on the Toolbox window)

Step 3: Drag the controls to the Userform: BSComboBox, BSListNox, BSButton, BSImageList

Step 4: Right click on the Userform => View Code. Now you have the widow to edit the code. Copy the code below:
'-------BEGIN COPY
'Author: Nguyen Duy Tuan - http://bluesofts.net - http://atoolspro.com
#If VBA7 Then
Private Declare PtrSafe Function DestroyIcon Lib "User32.dll" (ByVal HIcon As LongPtr) As Long
#Else
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
    #Else
        Dim hIcon() As Long, hSmIcon() As Long
    #End If
    Dim I&, n As Long, idx&
    'Setup ImageList for display icon in Combobox
    BSImageList1.ListImages.Clear
    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
    
    BSComboBox1.Clear
    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()
    BSImageList1.ListImages.Clear
End Sub
 
Private Sub BSComboBox1_OnSelect()
    Label2.Caption = BSComboBox1.Selected.Text
    Label2.Enabled = True
End Sub
 
'-------END COPY