Creation of a ComboBox drop-down menu for color selection in VBA

Creation of a ComboBox drop-down menu for color selection in VBA

Anonymous
Not applicable
8,726 Views
23 Replies
Message 1 of 24

Creation of a ComboBox drop-down menu for color selection in VBA

Anonymous
Not applicable

Hello ! 

 

I am looking for a way to create a ComboBox of this type in VBA in a UserForm : dd.PNG

There is quite a bit of Excel, but nothing like AutoCad. I understand how to fill it in text but I do not know how to create squares or shapes of color in front of the text describing the colors, my goal is to be able then to extract the selected color and to apply it to an entity.

 

Can you help me ?

 

0 Likes
Accepted solutions (1)
8,727 Views
23 Replies
Replies (23)
Message 21 of 24

Anonymous
Not applicable

Nice it work with "Application" soluce 🙂

 

 

I supposed i must do the same with :

Me.BackColor = cc.rgbResult

And replace by :

Application.BackColor = cc.rgbResult

 

 

When i do that and click on button, the windows for color selection finnaly appair ! Victory ! ^^

 

 

But when i click on one color and validate , a new (again... sorry guy ) error message return to the line : 

Application.BackColor = cc.rgbResult

"Execution error '438' 

property or method not supported by this object"

 

The only mean I found to make desappear the message is to put in commentary this part of code:

     'assign the selected colour as the form background
      Application.BackColor = cc.rgbResult

     'bonus .. assure the text remains readable regardless of colour by splitting out the respective
     'RGB values, and adjusting the text colour to contrast
      Call GetRBGFromCLRREF(cc.rgbResult, r, g, b)
      Call UpdateControlShadeSelection(r, g, b)

I don't know if it was a good idea ?

 

Then when I can select color and validate without any message error.

To check that the code works and to be able to implement it in my own other project I will want to get the value of the selected color. Do we agree that the value of the color is stored in the variables R G and B?

I unable to display their value ...

 

0 Likes
Message 22 of 24

Anonymous
Not applicable
Accepted solution

 

 


@Anonymous wrote:

Nice it work with "Application" soluce 🙂

 

 

I supposed i must do the same with :

Me.BackColor = cc.rgbResult

And replace by :

Application.BackColor = cc.rgbResult

 


Nope, you'd have to change

 

Me.BackColor = cc.rgbResult

to

Me.BackColor = CLng(cc.rgbResult)

 

 

But my understanding from your last post is that most of the code you showed is not of your actual interest while you only need a way to have the user pick a color and then use it in some way.

 

So, no need for using that color and change form controls background color (maybe even those Option1Option2Option3Check1 and Command1 controls are not actual controls of your form?)

 

hence, here's the updated code with some chosen color usage examples:

 

1) place the following at the very top of any module (hence, NOT in any UserForm code pane) 

Option Explicit

Public Declare PtrSafe Function CHOOSECOLOR Lib "comdlg32.dll" Alias "ChooseColorA" (lpcc As CHOOSECOLORSTRUCT) As LongPtr

Public Type CHOOSECOLORSTRUCT
    lStructSize As LongPtr
    hwndOwner As LongPtr
    hInstance As LongPtr
    rgbResult As LongPtr
    lpCustColors As String
    flags As LongPtr
    lCustData As LongPtr
    lpfnHook As LongPtr
    lpTemplateName As String
End Type

2) place the following in your UserForm code pane

 

Option Explicit

Private dwCustClrs(0 To 15) As Byte

'ChooseColor structure flag constants
Private Const CC_RGBINIT         As Long = &H1
Private Const CC_FULLOPEN        As Long = &H2
Private Const CC_PREVENTFULLOPEN As Long = &H4
Private Const CC_SOLIDCOLOR      As Long = &H80
Private Const CC_ANYCOLOR        As Long = &H100


Public Sub UserForm_Initialize()
    Dim cnt As Long
    With Me '<--| skip this block if your form doesn't actually have these controls
        .Option1.Caption = "Display normally"
        .Option1.Value = True
        .Option2.Caption = "Display with Define Custom Colors open"
        .Option3.Caption = "Disable Define Custom Colors button"
        .Check1.Caption = "Specify initial colour is form BackColor"
        .Command1.Caption = "Choose Color"
    End With
End Sub

Public Sub Command1_Click()
    Dim cc As CHOOSECOLORSTRUCT
    Dim r As Long
    Dim g As Long
    Dim b As Long
    
    Me.hide
    
    With cc
        'set the flags based on the check and option buttons  <--| skip this section if your form actually hasn't these controls
        .flags = CC_ANYCOLOR
        If Me.Option2.Value = True Then .flags = .flags Or CC_FULLOPEN
        If Me.Option3.Value = True Then .flags = .flags Or CC_PREVENTFULLOPEN
        If Me.Check1.Value = 1 Then .flags = .flags Or CC_RGBINIT
        
        'size of structure
        .lStructSize = Len(cc)
        
        'owner of the dialog
        .hwndOwner = Application.HWND
        
        'assign the custom colour selections
        .lpCustColors = StrConv(dwCustClrs, vbUnicode)
    End With

    If CHOOSECOLOR(cc) = 1 Then
        'get chosen color RGB values
        GetRBGFromCLRREF CLng(cc.rgbResult), r, g, b
   

' some chosen color usage examples

'--------------------------------
' example 1: show RGB values
MsgBox "chosen color RGB values are" & vbcrlf & vbcrlf & "red: " & vbTab & r & vbCrLf & "green: " & vbTab & g & vbCrLf & "blue: " & vbTab & b '--------------------------------

'--------------------------------
' example 2: color a user selected entity

' instance an AcCmColor object and set its RGB values to the chosen color ones
Dim color As AcadAcCmColor
Set color = AcadApplication.GetInterfaceObject("AutoCAD.AcCmColor.19")
color.SetRGB r, g, b

'make the user choose an entity
Dim ent As AcadEntity
Dim basePnt As Variant
ThisDrawing.Utility.GetEntity ent, basePnt, "Select an object"

'set selected entity color to the chosen one
ent.TrueColor = color
'--------------------------------

End If
End Sub

Public Sub GetRBGFromCLRREF(ByVal clrref As Long, r As Long, g As Long, b As Long)
'pass a hex colour, return the rgb components
b = (clrref \ 65536) And &HFF
g = (clrref \ 256) And &HFF
r = clrref And &HFF
End Sub 

 

Message 23 of 24

Anonymous
Not applicable

I do not know how to thank you. But in any case, big thank you to you!

 

This is exactly what I wanted to do and it work perfectly. 🙂

 

I will remove the different display option and it will be perfect, that for once I'll get it alone hahaha !

 

Creafter

 

0 Likes
Message 24 of 24

Anonymous
Not applicable

you are welcome

 

good coding!

0 Likes