DefaultValue Attribute for property of type Color

I've tried about 100 different ways of specifying a default value for a Color property to be displayed in a PropertyGrid. None of them have worked.

How do you specify a default value for a Color property using the [DefaultValue()]attribute



Answer this question

DefaultValue Attribute for property of type Color

  • Tryin2Bgood

    You must use the version that allows you to specify the type and a string argument. The string argument is the name of the color.

    [DefaultValue(typeof(Color), "Blue")]
    public Color MyColor
    {
    get { return m_Color; }
    set { m_Color = value; }
    }
    private Color m_Color = Color.Blue;

    Michael Taylor - 8/10/06


  • MaggieChan

    Thanks!
  • WishfulDoctor

    What about the case where the defalt value is a RGB value
  • Samoyed

    Use the encoded hex value:

    [DefaultValue(typeof(Color), "0x808080")]

    The above value will set the default to gray. Basically you can pass any string to this attribute that can be converted to the target type. Internally it uses the types TypeConverter to convert the string to the value. If that works then the default value will be set. For Color you can use strings containing decimal values, octals, &H hex values, etc. in addition to named colors.

    Michael Taylor - 8/10/06


  • DefaultValue Attribute for property of type Color