What worked for me for properties that I can't override is using the new
operator. For example, the MultiSelect
property on a ListView
control. I want MultiSelect
to default to false
, but I still want to be able to change it.
If I just set it to false
in the constructor, or in 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 designer notices that true
is the default, and so just doesn't set the property at all rather than explicitly setting it to what it thinks is already the default. But then the value ends up being false
instead, because that is what is set in the constructor.
To get around this issue 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 functioning MultiSelect
property that defaults to false
rather than true
, and the property can still be toggled on the new control.
EDIT: I encountered an issue having to do with using abstract forms. I've been using abstract form classes, with a concrete implementation that I switch to when I need to use the designer. I found that when I switched the class that I was inheriting from that the properties on my custom control would reset to the old defaults. I seem to have corrected this behaviour by setting properties to their defaults in the constructor of the custom control.
[DefaultValue(someValue)]
. Here's a link to the MSDN on it with example: msdn.microsoft.com/en-us/library/… – Bridge