I am creating a custom control with a black background but have some issues with the designer. Truth to be told I have a base control class that inherits from UserControl
and then some subclasses that represent the final controls that I will use in my GUI. In that base class I override the BackColor
property, add the DefaultValue
attribute and set the default value to BackColor
in the constructor. As an example my code looks something like this:
public partial class MyControl1 : UserControl
{
public MyControl1()
{
InitializeComponent();
BackColor = Color.Black;
}
[DefaultValue(typeof(Color),"Black")]
public override Color BackColor
{
get
{
return base.BackColor;
}
set
{
base.BackColor = value;
}
}
}
...
public partial class MyControl2 : MyControl1
{
public MyControl2()
{
InitializeComponent();
}
}
The thing is every time I open the designer for MyControl2
, BackColor
in the properties dialog reverts to System.Drawing.SystemColors.Control
and my control is painted grey. If I invoke Reset on BackColor
it properly returns to Color.Black
, though. Also, the designer doesn't serialize the change to System.Drawing.SystemColors.Control
until I make another change to the control.
So, what did I try?
I thought it could be related to
BackColor
being an ambient property so I tried adding the attributeAmbientValue(false)
. Of course it didn't work.I tried erasing the overridden property, leaving only
BackColor=Color.Black
in the constructor. Surprisingly it fixed the problem with the designer but now resetting the property reverted it to a default value ofSystem.Drawing.SystemColors.Control
. OverridingResetBackColor()
didn't solve this last problem.
By the way, I am working under Visual Studio 2010 and my project was created as a .NET 2.0 Windows Forms Application.
I would be glad whether anyone could help me to find whatever is wrong in my code. It is not something that would prevent me from finishing the project but it is pretty annoying. Thank you very much in advance!