Using a string array to place colors in graphics

I have a string array like so:
Var cc() as string = Array(“Black”, “White”, “Yellow”, “Brown”, “Green”)

And I want to use it to change colors in one of my canvas paint events.
I tried writing:
g.DrawingColor = Color.cc(x)
Or
g.DrawingColor = cc(x)
Or
Var PaintColor as Color = cc(x)
g.DrawingColor = PaintColor
But none of the above seem to be the right way, please educate me:)

i think you can use a Color Array and Add color in as example Color.Black

You can’t convert String values to colors like that. You could instead create an actual array of colors:

Var cc() As Color = Array(Color.Black, Color.White, Color.Yellow, Color.Brown, Color.Green)
Var x As Integer = 2
g.DrawingColor = cc(x)
g.FillRectangle(0, 0, g.Width, g.Height)
1 Like

That is great but I created a user selection that shows the color names (from the array) to the user and lets him choose what color he wants, once the user click one it should be pushed to the paint event as color.
If there is no built-in method to convert text/string into color property I can create a second array that hosts the same values as colors and when the user select one of them I push the same index number but from the other (color) array.
Just forces me to duplicate everything and I have about ten of those :wink:

check if your selection list have a rowtag you could place something hidden beside the text.

Thanks for the idea @MarkusR, much appreciated!
However, I made it all from scratch using an empty canvas (the green arrow shows what I need to push to where)

how about a dictionary? the color name is the key, the value the color.
if your ui is only english it should work.
your color chooser seems to be a custom control.

Yes, I made all the controls from scratch as I said earlier, a bit of crazy work but I made it extremely modular so I can reuse them elsewhere for any purpose in any size.

Your idea sounds like a good solution/way around it, thanks!

All the best,
Sagi

seems you need a method “ColorByName” that you can pass “White” to and it returns &cFFFFFF00

1 Like

good then you can easy extend it.

1 Like

Love the custom colour control. Very nice.

And yes, dictionary is what you’re after. Perfect for this.

1 Like