NamedColors problem

  • Thread starter Thread starter Tim Mulholland
  • Start date Start date
T

Tim Mulholland

I am having a problem where a color i select, and convert to a string for
storage in an XML file is not being converted back properly. The following
is a small example that illustrates the problem.The color seems to show up
properly, but it has lost its IsNamedColor property. Is it possible to get
this back or is there any different way i should be doing any of this?

string s = System.Drawing.SystemColors.Control.ToArgb().ToString();
bool b = System.Drawing.SystemColors.Control.IsNamedColor; //Returns true
ColorConverter c = new ColorConverter();
Color clr = (System.Drawing.Color)(c.ConvertFromString(s));
bool b2 = clr.IsNamedColor; //Returns false

Thanks in advance,

Tim
 
After doing a little more evaluation, it seems that MOST of the SystemColors
colors have this problem, whereas all of the "plain" Colors do NOT have this
problem.
Here is an example program to see what i mean. Can anyone help me figure out
what i'm doing wrong?

KnownColor t = new KnownColor();
foreach (KnownColor kc in System.Enum.GetValues(t.GetType()))
{
ColorConverter cc = new ColorConverter();
Color c = Color.FromName(kc.ToString());
string s = c.ToArgb().ToString();
bool b = System.Drawing.SystemColors.Control.IsNamedColor;
Color clr = (System.Drawing.Color)(cc.ConvertFromString(s));
bool b2 = clr.IsNamedColor;
System.Diagnostics.Trace.WriteLine(c.ToString() + ": " + (b ==
b2).ToString());
}

Tim
 
Tim,

You are storing the wrong string values. If you want to convert a color
to a string, you should not be using the RGB value. If the value is an RGB
value, then it doesn't know that it is a named color value. In order to get
the correct color, you should use the ConvertToString method on the
ColorConverter class to get the string representation of the color. Once
you have this, if you pass it to the ConvertFromString method, then you will
find that IsNamedColor returns true.

Hope this helps.
 
I knew it would be something simple like that :)
That's what i get for believing what i read on the web ;)
Thanks!

Tim

Nicholas Paldino said:
Tim,

You are storing the wrong string values. If you want to convert a color
to a string, you should not be using the RGB value. If the value is an RGB
value, then it doesn't know that it is a named color value. In order to get
the correct color, you should use the ConvertToString method on the
ColorConverter class to get the string representation of the color. Once
you have this, if you pass it to the ConvertFromString method, then you will
find that IsNamedColor returns true.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Tim Mulholland said:
After doing a little more evaluation, it seems that MOST of the SystemColors
colors have this problem, whereas all of the "plain" Colors do NOT have this
problem.
Here is an example program to see what i mean. Can anyone help me figure out
what i'm doing wrong?

KnownColor t = new KnownColor();
foreach (KnownColor kc in System.Enum.GetValues(t.GetType()))
{
ColorConverter cc = new ColorConverter();
Color c = Color.FromName(kc.ToString());
string s = c.ToArgb().ToString();
bool b = System.Drawing.SystemColors.Control.IsNamedColor;
Color clr = (System.Drawing.Color)(cc.ConvertFromString(s));
bool b2 = clr.IsNamedColor;
System.Diagnostics.Trace.WriteLine(c.ToString() + ": " + (b ==
b2).ToString());
}

Tim
show
 
Back
Top