Connecting Tech Pros Worldwide Forums | Help | Site Map

NamedColors problem

Tim Mulholland
Guest
 
Posts: n/a
#1: Jul 21 '05
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().ToStr ing();
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



Tim Mulholland
Guest
 
Posts: n/a
#2: Jul 21 '05

re: NamedColors problem


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 Mulholland" <Mulholland.NoSpam@alumni.virginia.edu> wrote in message
news:e6q$z4ZmDHA.708@TK2MSFTNGP10.phx.gbl...[color=blue]
> 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().ToStr ing();
> 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
>
>[/color]


Nicholas Paldino [.NET/C# MVP]
Guest
 
Posts: n/a
#3: Jul 21 '05

re: NamedColors problem


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]
- mvp@spam.guard.caspershouse.com

"Tim Mulholland" <Mulholland.NoSpam@alumni.virginia.edu> wrote in message
news:%23Iav$BamDHA.1728@TK2MSFTNGP09.phx.gbl...[color=blue]
> After doing a little more evaluation, it seems that MOST of the[/color]
SystemColors[color=blue]
> colors have this problem, whereas all of the "plain" Colors do NOT have[/color]
this[color=blue]
> problem.
> Here is an example program to see what i mean. Can anyone help me figure[/color]
out[color=blue]
> 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 Mulholland" <Mulholland.NoSpam@alumni.virginia.edu> wrote in message
> news:e6q$z4ZmDHA.708@TK2MSFTNGP10.phx.gbl...[color=green]
> > I am having a problem where a color i select, and convert to a string[/color][/color]
for[color=blue][color=green]
> > storage in an XML file is not being converted back properly. The[/color][/color]
following[color=blue][color=green]
> > is a small example that illustrates the problem.The color seems to show[/color][/color]
up[color=blue][color=green]
> > properly, but it has lost its IsNamedColor property. Is it possible to[/color][/color]
get[color=blue][color=green]
> > this back or is there any different way i should be doing any of this?
> >
> > string s = System.Drawing.SystemColors.Control.ToArgb().ToStr ing();
> > bool b = System.Drawing.SystemColors.Control.IsNamedColor; //Returns[/color][/color]
true[color=blue][color=green]
> > ColorConverter c = new ColorConverter();
> > Color clr = (System.Drawing.Color)(c.ConvertFromString(s));
> > bool b2 = clr.IsNamedColor; //Returns false
> >
> > Thanks in advance,
> >
> > Tim
> >
> >[/color]
>
>[/color]


Tim Mulholland
Guest
 
Posts: n/a
#4: Jul 21 '05

re: NamedColors problem


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 [.NET/C# MVP]" <mvp@spam.guard.caspershouse.com> wrote in
message news:uB4okbamDHA.2200@TK2MSFTNGP12.phx.gbl...[color=blue]
> Tim,
>
> You are storing the wrong string values. If you want to convert a[/color]
color[color=blue]
> to a string, you should not be using the RGB value. If the value is an[/color]
RGB[color=blue]
> value, then it doesn't know that it is a named color value. In order to[/color]
get[color=blue]
> 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[/color]
will[color=blue]
> find that IsNamedColor returns true.
>
> Hope this helps.
>
>
> --
> - Nicholas Paldino [.NET/C# MVP]
> - mvp@spam.guard.caspershouse.com
>
> "Tim Mulholland" <Mulholland.NoSpam@alumni.virginia.edu> wrote in message
> news:%23Iav$BamDHA.1728@TK2MSFTNGP09.phx.gbl...[color=green]
> > After doing a little more evaluation, it seems that MOST of the[/color]
> SystemColors[color=green]
> > colors have this problem, whereas all of the "plain" Colors do NOT have[/color]
> this[color=green]
> > problem.
> > Here is an example program to see what i mean. Can anyone help me figure[/color]
> out[color=green]
> > 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 Mulholland" <Mulholland.NoSpam@alumni.virginia.edu> wrote in[/color][/color]
message[color=blue][color=green]
> > news:e6q$z4ZmDHA.708@TK2MSFTNGP10.phx.gbl...[color=darkred]
> > > I am having a problem where a color i select, and convert to a string[/color][/color]
> for[color=green][color=darkred]
> > > storage in an XML file is not being converted back properly. The[/color][/color]
> following[color=green][color=darkred]
> > > is a small example that illustrates the problem.The color seems to[/color][/color][/color]
show[color=blue]
> up[color=green][color=darkred]
> > > properly, but it has lost its IsNamedColor property. Is it possible to[/color][/color]
> get[color=green][color=darkred]
> > > this back or is there any different way i should be doing any of this?
> > >
> > > string s = System.Drawing.SystemColors.Control.ToArgb().ToStr ing();
> > > bool b = System.Drawing.SystemColors.Control.IsNamedColor; //Returns[/color][/color]
> true[color=green][color=darkred]
> > > ColorConverter c = new ColorConverter();
> > > Color clr = (System.Drawing.Color)(c.ConvertFromString(s));
> > > bool b2 = clr.IsNamedColor; //Returns false
> > >
> > > Thanks in advance,
> > >
> > > Tim
> > >
> > >[/color]
> >
> >[/color]
>
>[/color]


Closed Thread


Similar .NET Framework bytes