Convert text box text into Argb argument
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 usa开发者_开发知识库ble by FromArgb?
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.
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!)
精彩评论