Reapz Posted December 17, 2002 Posted December 17, 2002 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.... Button1.BackColor = Color.Red Button2.BackColor = Color.Blue Button3.BackColor = Color.Green Button4.BackColor = Color.Black But if I try this... 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? Quote I'm getting the hang of this now... No really I am!
Moderators Robby Posted December 17, 2002 Moderators Posted December 17, 2002 try this ... dim Col1 as Color.Red Button1.BackColor = Col1 Quote Visit...Bassic Software
Reapz Posted December 17, 2002 Author Posted December 17, 2002 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!;) Quote I'm getting the hang of this now... No really I am!
Reapz Posted December 17, 2002 Author Posted December 17, 2002 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... Quote I'm getting the hang of this now... No really I am!
*Experts* Nerseus Posted December 17, 2002 *Experts* Posted December 17, 2002 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 Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Reapz Posted December 17, 2002 Author Posted December 17, 2002 Ah ha! Nice one mate! Color.FromName() worked like a charm. Cheers! :D Quote I'm getting the hang of this now... No really I am!
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.