DefaultValue for System.Drawing.SystemColors
-
09-06-2019 - |
Question
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?
Solution
This may help: http://support.microsoft.com/kb/311339 -- a KB article entitled "MSDN documentation for the DefaultValueAttribute class may be confusing"
OTHER TIPS
You need to change first argument from SystemColors
to Color
.
It seems that there is no type converter for the SystemColors
type, only for the Color
type.
[DefaultValue(typeof(Color),"InactiveBorder")]
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;
}
}
}