开发者

Convert text box text into Argb argument

开发者 https://www.devze.com 2022-12-26 19:26 出处:网络
I have been looking into coloring objects like ellipses with code such as SolidBrush trnsRedBrush = new SolidBrush(Color.FromArgb(0x78FF0000));

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

0

精彩评论

暂无评论...
验证码 换一张
取 消