views:

663

answers:

3

I have a line color property in my custom grid control. I want it to default to Drawing.SystemColors.InactiveBorder. I tried:

[DefaultValue(typeof(System.Drawing.SystemColors), "InactiveBorder")]
public Color LineColor { get; set; }

But it doesn't seem to work. How do I do that with the default value attribute?

+1  A: 

You need to change first argument from SystemColors to Color.
It seems that there is no type converter for SystemColors type, only for Color type.

[DefaultValue(typeof(Color),"InactiveBorder")]
aku
+4  A: 

This may help: http://support.microsoft.com/kb/311339 -- a KB article entitled "MSDN documentation for the DefaultValueAttribute class may be confusing"

Matt Bishop
+1  A: 

According to the link Matt posted, the DefaultValue attribute doesn't set the default value of the property, it just lets the form designer know that the property has a default value. If you change a property from the default value it is shown as bold in the properties window.

You can't set a default value using automatic properties - you'll have to do it the old-fashioned way:

class MyClass
{
    Color lineColor = SystemColors.InactiveBorder;

    [DefaultValue(true)]
    public Color LineColor {
        get {
            return lineColor;
        }

        set {
            lineColor = value;
        }
    }
}
roomaroo