text to system.drawing.color mapping

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have an xml file that I use to set values of DataGridStyles - header text, column width etc
I would also like to make the background color selected other items data driven

What is the smart way to associate and apply a color
string mybackground = "SandyBrown
but ...
objDGTS.BackColor=System.Drawing.Color.SandyBrown

I want to do 2 things, validate mybackground is one of the named colors - then
be able to write - somehow ...
objDGTS.BackColor=??( System.Drawing.Color)mybackground

Thanks Andrew
 
andrewcw said:
I have an xml file that I use to set values of DataGridStyles - header text, column width etc,
I would also like to make the background color selected other items data driven.

What is the smart way to associate and apply a color ?
string mybackground = "SandyBrown"
but ....
objDGTS.BackColor=System.Drawing.Color.SandyBrown;

I want to do 2 things, validate mybackground is one of the named colors - then
be able to write - somehow ....
objDGTS.BackColor=??( System.Drawing.Color)mybackground.

Hi andrewcw,

private void button1_Click(object sender, System.EventArgs e)
{
//check if "SandyBrown" is a valid KnownColor
if (System.Enum.IsDefined(typeof(KnownColor), "SandyBrown"))
{
//parse and cast "SandyBrown" to a KnownColor-Object
//use Color.FromKnownColor to assign the color to your grid.
objDGTS.BackColor = Color.FromKnownColor((KnownColor)
System.Enum.Parse(typeof(KnownColor), "SandyBrown"));
}
}

Cheers

Arne Janning
 
Andrew,

You will want to use the ColorConverter class (which derives from
TypeConverter). You want to call the ConvertFromString method on the
ColorConverter class, which will give you the Color instance from the
string.

If you want to check to see if the string is valid, I believe you can
call the IsValid method, passing the color of the string.

Hope this helps.
 
Back
Top