I'm looking for help setting a new default property value for an inherited control in Visual Studio:

class NewCombo : System.Windows.Forms.ComboBox
  public NewCombo() { DropDownItems = 50; }

The problem is that the base class property "DropDownItems" has a 'default' attribute set on it that is a different value (not 50). As a result, when I drag the control onto a form, the designer file gets an explicit 'mycontrol.DropDownItems = 50;' line.

At first this doesn't matter. But if later I change my inherited class to 'DropDownItems = 45;' in the constructor this does not affect any of the controls on any form since all those designer files still have the value 50 hardcoded in them. And the whole point was to have the value set in one place so I can deal with the customer changing his mind.

Obviously, if I were creating my own custom property in the subclass, I could give it its own designer default attribute of whatever I wanted. But here I'm wanting to change the default values of properties in the base. Is there any way to apply Visual Studio attributes to a base class member? Or is there some other workaround to get the result I want?

Thanks for any help

+2  A: 

In your derived class you need to either override (or shadow using new) the property in question and then re-apply the default value attribute.

Andrew Peters

Well that was simple...guess it doesn't make a difference that the base member isn't virtual/overridable.


Just came across this too and the solution above does work, but... only in Designer it seems. I inherit from DataGridView, and want to eg. change the default border style to None. Override doesn't work ("cannot override inherited member System.Windows.Forms.DataGridView.BorderStyle.get' because it is not marked virtual, abstract, or override"), and the code below does not produce the desired result.

[DefaultValue(typeof(BorderStyle), "None")]
public override BorderStyle BorderStyle { get; set; }

Any ideas how to remedy the problem? Perhaps I'm doing something very wrong?

I did see the Daniel Stutzbach's "ApplyDefaultValues" but for some properties it generates erorrs - I was hoping there was an automated (as in 'from-Microsoft-with-love') way of doing this.
I believe DefaultValue only describes the property, it doesnt affect the value of the property. I'll try to find the source for that info.
Found it, see the note in the Remarks section -
Seems like a lot of people are confused, there is a followup from support -
Based on other answers in this question, sounds like you want to do `public new BorderStyle BorderStyle { get; set; }` and return your default value in the new property.
Thanks for the links! So if I understand correctly, DefaultValueAttribute is not used at runtime, but only by the Designer? If that's the case then I guess I would need to do `[DefaultValue(typeof(BorderStyle), "None")] public new BorderStyle BorderStyle { get; set; }` and then in the constructor do `BorderStyle = BorderStyle.None;`? Or have a `private BorderStyle _borderStyle = BorderStyle.None;` instead of the default property. It's not entirely the elegant, perfect-world solution I thought was possible.
Well, the `private BorderStyle _borderStyle = BorderStyle.None;` strategy doesn't work. Are we thus left with initializing stuff our way in good ol' constructor?