I have a user Form that has a combo box in it Comb_colorPick_Change
, each item in the combo box represents a color as in this picture
I defined an enum which stores some word color constants (wdRed
, wdGreen
, …etc), I wanted to change the back color of the combo box using a bitwise operation (AND) between the Sum of all enum constants (all) and the combobox item index Comb_colorPick.BackColor = (Comb_colorPick.ListIndex + 1) And All
, this operation results a number (ex: 3) which corresponds to a color (ex: wdRed
) BUT I need the result number to be (wdRed) instead of (3). i used select case before to change the back color but i'm looking for another method
My main question is:
How do I define a two way enum where I can get a number by providing a constant name and vice versa.
my other questions
- Is it possible to loop through a public enum, ex: WdColor enum
- How to use “bitwise enumerations”, a practical example would be appreciated
Here is my code:
Private Enum MyColors
Red = wdRed
Green = wdGreen
Blue = wdBlue
Yellow = wdYellow
Turquoise = wdTurquoise
BrightGreen = wdBrightGreen
Pink = wdPink
Violet = wdViolet
All = wdRed Or wdGreen Or wdBlue Or wdYellow Or wdTurquoise Or wdBrightGreen Or wdPink Or wdViolet
End Enum
Private Sub Comb_colorPick_Change()
Dim co As MyColors
co = All
'Comb_colorPick.BackColor = (Comb_colorPick.ListIndex + 1) And co '<=== i need this to be a constant name
Label1.Caption = "Bitwise Result = " & ((Comb_colorPick.ListIndex + 1) And co)
End Sub
Private Sub UserForm_Activate()
' fill the combo during user form activation event
With Comb_colorPick
.AddItem "Red", 0
.AddItem "Green", 1
.AddItem "Blue", 2
.AddItem "Yellow", 3
.AddItem "Turquoise", 4
.AddItem "BrightGreen", 5
.AddItem "Pink", 6
.AddItem "Violet", 7
.Value = "BrightGreen"
End With
End Sub