views:

115

answers:

1

When I right-click on my custom UserControl's BackColor property in the property-grid, then click Reset, I would like the BackColor property to change to (for example) Color.LightGreen, and the property value to appear un-bolded, to indicate that it is the default value.

Currently, I know I can do this:

public override void ResetBackColor() {
    BackColor = Color.LightGreen;
}

Which works as far as setting it to LightGreen on reset. But it still appears bolded in the property-grid, indicating that the current value is not the default.

I notice that the Control class has a static read-only property, DefaultBackColor. Unfortunately, since it's static, I cannot override it.

Is there some way to get all the functionality I want?

+2  A: 

You can achieve this by using the DefaultValue attribute:

public UserControl1()
{
    InitializeComponent();
    this.BackColor = Color.LightGreen;
}

[DefaultValue(typeof(Color), "LightGreen")]
public override Color BackColor
{
    get
    {
        return base.BackColor;
    }
    set
    {
        base.BackColor = value;
    }
}
Fredrik Mörk