tags:

views:

45

answers:

2

I have been looking into coloring objects like ellipses with code such as

        SolidBrush trnsRedBrush = new SolidBrush(Color.FromArgb(0x78FF0000));

I'd like to play around further with this by entering FromArgb's argument into a textbox on a form, then using the textbox to set the Brush's color. How would I convert the textbox's text into an argument usable by FromArgb?

+3  A: 
someTextBox.Text = "AAFFBBDD";
int param = int.Parse(someTextBox.Text, NumberStyles.AllowHexSpecifier);
SolidBrush trnsRedBrush = new SolidBrush(Color.FromArgb(param));

You could shorten this of course.

Edit: Keep in mind if you type something bad this could throw an exception. There are "TryParse" variants to allow you to handle the situation to your liking. I won't make a specific recommendation because it depends on the context and scenario.

AaronLS
Worked. Thanks!
Jimmy
+1  A: 

If you are planning on entering hexadecimal values into the textbox, why not just do:

SolidBrush trnsRedBrush = new SolidBrush(Color.FromArgb(Convert.ToInt32(textBox.Text), 16));

Edit: Have to cast the value to an int first. (oops!)

JYelton
Where did you find documentation indicating that FromArgb would accept a string as a parameter?
AaronLS
AaronLS, you're right, it doesn't. It would have to be parsed first as in your answer.
JYelton