What worked for me for properties that I cannot override is using the new operator. For example, the MultiSelect property in a ListView control. I want MultiSelect to be false by default, but I still want to change it.
If I just set it to false in the constructor or in the InitializeComponent , the problem (I think) is that the default value is still true , so if I set the value to true in the designer, the developer notices that true is the default value. and therefore simply does not set the property at all, and does not explicitly set it to what, in his opinion, is already the default value. But then the value becomes false instead, because that is what is set in the constructor.
To get around this problem, I used the following code:
/// <summary>Custom ListView.</summary> public sealed partial class DetailsListView : ListView { ... [DefaultValue(false)] public new bool MultiSelect { get { return base.MultiSelect; } set { base.MultiSelect = value; } }
This allows the control to still have a valid MultiSelect property, which defaults to false rather than true , and the property can still be switched to the new control.
EDIT: I ran into a problem using abstract forms. I used abstract form classes, with a specific implementation that I switch with when I need to use the constructor. I found that when I switched the class that I inherited from the fact that the properties of my custom control would reset to the old default values. I seem to have fixed this behavior by setting the properties to their default values ββin the constructor of the custom control.
Dave cousineau
source share