NamedColors problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tim Mulholland

    NamedColors problem

    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.Co ntrol.ToArgb(). ToString();
    bool b = System.Drawing. SystemColors.Co ntrol.IsNamedCo lor; //Returns true
    ColorConverter c = new ColorConverter( );
    Color clr = (System.Drawing .Color)(c.Conve rtFromString(s) );
    bool b2 = clr.IsNamedColo r; //Returns false

    Thanks in advance,

    Tim


  • Tim Mulholland

    #2
    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.Get Values(t.GetTyp e()))
    {
    ColorConverter cc = new ColorConverter( );
    Color c = Color.FromName( kc.ToString());
    string s = c.ToArgb().ToSt ring();
    bool b = System.Drawing. SystemColors.Co ntrol.IsNamedCo lor;
    Color clr = (System.Drawing .Color)(cc.Conv ertFromString(s ));
    bool b2 = clr.IsNamedColo r;
    System.Diagnost ics.Trace.Write Line(c.ToString () + ": " + (b ==
    b2).ToString()) ;
    }

    Tim

    "Tim Mulholland" <Mulholland.NoS pam@alumni.virg inia.edu> wrote in message
    news:e6q$z4ZmDH A.708@TK2MSFTNG P10.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.Co ntrol.ToArgb(). ToString();
    > bool b = System.Drawing. SystemColors.Co ntrol.IsNamedCo lor; //Returns true
    > ColorConverter c = new ColorConverter( );
    > Color clr = (System.Drawing .Color)(c.Conve rtFromString(s) );
    > bool b2 = clr.IsNamedColo r; //Returns false
    >
    > Thanks in advance,
    >
    > Tim
    >
    >[/color]


    Comment

    • Nicholas Paldino [.NET/C# MVP]

      #3
      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 ConvertFromStri ng method, then you will
      find that IsNamedColor returns true.

      Hope this helps.


      --
      - Nicholas Paldino [.NET/C# MVP]
      - mvp@spam.guard. caspershouse.co m

      "Tim Mulholland" <Mulholland.NoS pam@alumni.virg inia.edu> wrote in message
      news:%23Iav$Bam DHA.1728@TK2MSF TNGP09.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.Get Values(t.GetTyp e()))
      > {
      > ColorConverter cc = new ColorConverter( );
      > Color c = Color.FromName( kc.ToString());
      > string s = c.ToArgb().ToSt ring();
      > bool b = System.Drawing. SystemColors.Co ntrol.IsNamedCo lor;
      > Color clr = (System.Drawing .Color)(cc.Conv ertFromString(s ));
      > bool b2 = clr.IsNamedColo r;
      > System.Diagnost ics.Trace.Write Line(c.ToString () + ": " + (b ==
      > b2).ToString()) ;
      > }
      >
      > Tim
      >
      > "Tim Mulholland" <Mulholland.NoS pam@alumni.virg inia.edu> wrote in message
      > news:e6q$z4ZmDH A.708@TK2MSFTNG P10.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.Co ntrol.ToArgb(). ToString();
      > > bool b = System.Drawing. SystemColors.Co ntrol.IsNamedCo lor; //Returns[/color][/color]
      true[color=blue][color=green]
      > > ColorConverter c = new ColorConverter( );
      > > Color clr = (System.Drawing .Color)(c.Conve rtFromString(s) );
      > > bool b2 = clr.IsNamedColo r; //Returns false
      > >
      > > Thanks in advance,
      > >
      > > Tim
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • Tim Mulholland

        #4
        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.c om> wrote in
        message news:uB4okbamDH A.2200@TK2MSFTN GP12.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 ConvertFromStri ng method, then you[/color]
        will[color=blue]
        > find that IsNamedColor returns true.
        >
        > Hope this helps.
        >
        >
        > --
        > - Nicholas Paldino [.NET/C# MVP]
        > - mvp@spam.guard. caspershouse.co m
        >
        > "Tim Mulholland" <Mulholland.NoS pam@alumni.virg inia.edu> wrote in message
        > news:%23Iav$Bam DHA.1728@TK2MSF TNGP09.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.Get Values(t.GetTyp e()))
        > > {
        > > ColorConverter cc = new ColorConverter( );
        > > Color c = Color.FromName( kc.ToString());
        > > string s = c.ToArgb().ToSt ring();
        > > bool b = System.Drawing. SystemColors.Co ntrol.IsNamedCo lor;
        > > Color clr = (System.Drawing .Color)(cc.Conv ertFromString(s ));
        > > bool b2 = clr.IsNamedColo r;
        > > System.Diagnost ics.Trace.Write Line(c.ToString () + ": " + (b ==
        > > b2).ToString()) ;
        > > }
        > >
        > > Tim
        > >
        > > "Tim Mulholland" <Mulholland.NoS pam@alumni.virg inia.edu> wrote in[/color][/color]
        message[color=blue][color=green]
        > > news:e6q$z4ZmDH A.708@TK2MSFTNG P10.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.Co ntrol.ToArgb(). ToString();
        > > > bool b = System.Drawing. SystemColors.Co ntrol.IsNamedCo lor; //Returns[/color][/color]
        > true[color=green][color=darkred]
        > > > ColorConverter c = new ColorConverter( );
        > > > Color clr = (System.Drawing .Color)(c.Conve rtFromString(s) );
        > > > bool b2 = clr.IsNamedColo r; //Returns false
        > > >
        > > > Thanks in advance,
        > > >
        > > > Tim
        > > >
        > > >[/color]
        > >
        > >[/color]
        >
        >[/color]


        Comment

        Working...