You are here ComboBox have color in item - BSAC Controls

ComboBox have color in item - BSAC Controls

Program VBA create ComboBox have color in item with BSComboBox in 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: In sheet, column A, rtatr row 1 you format interior 10 cells for any color. Rename sheet as "COLOR"

Step 5: Drag controls to userform: BSComboBox

Step 6: Right click on userform -> View Code. Now you have widow to edit code. Copy below code:
'-------BEGIN COPY
'Author: Nguyen Duy Tuan - -
Private Sub BSComboBox1_OnDrawItem(ByVal Index As Long, ByVal Left As Long, ByVal Top As Long, _
                                    ByVal Right As Long, ByVal Bottom As Long, _
                                    ByVal State As BSAC.TBSOwnerDrawState)
    BSComboBox1.Canvas.Brush.Color = CLng(BSComboBox1.Items(Index).Key)
    BSComboBox1.Canvas.FillRect Left, Top, Right - Left, Bottom ' - Top
    BSComboBox1.Canvas.TextOut Left, Top, BSComboBox1.Items(Index).Text
End Sub
Private Sub BSComboBox1_OnSelect()
    Range("e1:p25").Interior.Color = CLng(BSComboBox1.Selected.Key)
End Sub
Private Sub UserForm_Initialize()
    Dim I&, sh As Worksheet
    Set sh = Sheets("COLOR")
    BSComboBox1.Style = csOwnerDrawVariable
    BSComboBox1.ItemHeight = 30
    For I = 1 To 10
        BSComboBox1.Items.Add sh.Cells(I, 1).Value, , , sh.Cells(I, 1).Interior.Color
    Next I
End Sub
'-------END COPY

Download source code