Colours from strings...

Reapz

Regular
Joined
Dec 15, 2002
Messages
74
Quick question... I have 4 strings which hold colours that represent the BackColors of 4 buttons. They are Red, Blue, Green and Black and lets say the are called. Col1, Col2, Col3 and Col4. These values will be subject to change but not to any specific colours which is why they are stored in strings.

Now I know this works....

Visual Basic:
Button1.BackColor = Color.Red
Button2.BackColor = Color.Blue
Button3.BackColor = Color.Green
Button4.BackColor = Color.Black

But if I try this...

Visual Basic:
Button1.BackColor = Color.Col1
Button2.BackColor = Color.Col2
Button3.BackColor = Color.Col3
Button4.BackColor = Color.Col4

...it doesn't work. To be honest I didn't expect it to. :D

I there a simple way to do this or will I have to resort to a massive If... block?
 
DOH!
(Feels accutely embarrassed! )

That just proves that I've been sitting in front of this damn machine for far too long. The strings are retrieved from a text file but I never considered saving the info with the Color. attached in the first place.

I think I'll go get some sleep now...

...or maybe some coffee then I can keep going! Rah!!! :D

Thanks m8 even if it is only for highlighting my stoopidity!;)
 
Bugger I got overexcited!

I tried putting, for example, Color.Red in the textfile and then retrieving it and storing it in Col1. But the code (Button1.BackColor = Col1) still won't work because i'm trying to use a string as a color.

Sigh....

More coffee I think...
 
Two methods of the Color class will help:
Color.FromName()
and
Color.FromArgb()

FromName takes a string, such as "Red" or "ActiveCaption".

FromArgb takes individual red, green, and blue values (with optional alpha). Or you can pass in a hex string, converted to an int. For example:
Color.FromArgb(Convert.ToInt32("0xFF8020", 16))

This converts the hex string "0xFF8020" to red=255, green=128, blue=32.

-ner
 
Back
Top