I’ve got a Compact Framework (3.5) control and I’d like it have a default color of SystemColors.Window, similar in apprearance to an edit control. The problem I’m having is Visual Studio (2008) always renders that control on the design surface with a default BackColor of Control. I assume it is either pulling it from the default back color of the base Control, from the parent, or from the Site.
Anyone have any idea how to specify or otherwise inform Visual Studio what the default BackColor should be?
public class MoneyInput : Control
{
public MoneyInput()
{
base.BackColor = SystemColors.Window;
base.ForeColor = SystemColors.WindowText;
}
public override Color BackColor
{
get
{
return base.BackColor;
}
set
{
base.BackColor = value;
Repaint();
}
}
}
And here is the appropriate section of my DesignTimeAttributes.xmta file. Note that I don’t really want the default color to White as you see below, I was just trying to get anything to work:
<DesktopCompatible>true</DesktopCompatible>
<Property Name="BackColor">
<Browsable>true</Browsable>
<DesignerSerializationVisibility>
DesignerSerializationVisibility.Visible
</DesignerSerializationVisibility>
<DefaultValue>
<Type>System.Drawing.Color</Type>
<Value>System.Drawing.Color.White</Value>
</DefaultValue>
<Category>Appearance</Category>
<Description>The background color of the control</Description>
</Property>
Just to add some more progress (or lack thereof), I added a log to the control so that I can see what is going on when I drop the control on the design surface. The write to the BackColor in the constructor does take effect and changes the color from Control to Window. But then something in the designer infrastructure sets the color (via the property) back to Control. This happens after the ctor but before the OnResize and OnHandleCreated. Now if the control serialized the BackColor property into the designer with the correct value then it would be safely set back, but the designer doesn’t contain default values.
One last edit, I think the default value syntax for specifiying an enum (as opposed to a base type) is not correct. I am using the solution I posted below, but it is a hack and hopefully the next person that comes along can solve it for real.
I’m hoping to get a real answer still, but I’ll put down here my work-around solution: