问题描述
我在寻找帮助在Visual Studio继承的控制设置一个新的默认属性值:
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; }
}
现在的问题是基类财产 DropDownItems
有一个默认属性,就可以设置为不同的值(不是50)。这样一来,当我控制拖到形式,设计文件中得到明确的 mycontrol.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.
起初,这并不重要。但是,如果后来我改变我的继承类 DropDownItems = 45;
在构造函数中这并不影响对任何形式的任何控件,因为所有这些设计文件仍然具有值50硬codeD在其中。而整个的一点是要在一个地方设置的值,所以我可以跟客户打交道改变了主意。
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 hard-coded 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.
显然,如果我在子类中创建自己的自定义属性,我可以给它什么的,我想自己的名牌默认属性。但在这里,我想改变在基础属性的默认值。有什么办法到Visual Studio属性适用于基类成员?或者是有一些其他的变通方法来得到我想要的结果?
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?
推荐答案
在派生类中,你需要或者重写(或阴影使用的新的)的问题,则该属性重新应用默认value属性。
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.
这篇关于Visual Studio中 - 新"默认"为继承控件的属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!