Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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?

I'm getting the hang of this now... No really I am!
Posted

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!;)

I'm getting the hang of this now... No really I am!
Posted

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...

I'm getting the hang of this now... No really I am!
  • *Experts*
Posted

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

"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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...