How can I convert the string ‘Red’ into the color Color.Red, and vice versa

Platform: WinForms| Category: Strings

You can use the TypeDescriptor class to handle this.

	Color redColor = (Color)TypeDescriptor.GetConverter(typeof(Color)).ConvertFromString('Red');
	string redString = TypeDescriptor.GetConverter(typeof(Color)).ConvertToString(Color.Red);

You can also use code such as

	System.Drawing.ColorConverter ccv = new System.Drawing.ColorConverter();
	this.BackColor = (Color) ccv.ConvertFromString('Red');

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.