I recently had the need to write a version of the Windows NumericUpDown control which could highlight whether a value was mandatory. It needed to do this by changing the back colour of the control. I thought this would be simple enough, but in trying to do so, I find that it has a wierd side-effect of not quite drawing all the control.
Using the code below, I am seeing this:
When I drop a control on a Windows form, and change the BackColor property (ie. to Blue), the whole of the control's number part changes colour. If, alternatively, I change my "IsMandatory" property, not quite all of the back colour changes (it leaves a border). So, if I change the BackColor to Blue, and then set IsMandatory to True, I get a LightBlue control (the mandatory colour) with a Blue border.
I cannot see why that should be, given that they both use the same code.
Ideas or explanations greatfully received.
public partial class MyNumericUpDown : NumericUpDown
{
private Boolean _isMandatory = false;
private Color _mandatoryBackColor = Color.LightBlue;
private Color _backColor = Color.FromKnownColor(KnownColor.Window);
[DefaultValue(typeof(Color), "Window"), Description("Overridden property")]
override public Color BackColor
{
get { return _backColor; }
set
{
_backColor = value;
MyResetColors();
}
}
[DefaultValue(typeof(Color), "LightBlue"), Category("Appearance")]
public Color MandatoryBackColor
{
get {return _mandatoryBackColor;}
set
{
_mandatoryBackColor = value;
MyResetColors();
}
}
[DefaultValue(false), Category("Behavior")]
public Boolean IsMandatory
{
get { return _isMandatory; }
set
{
_isMandatory = value;
MyResetColors();
}
}
private void MyResetColors()
{
base.BackColor = (this.IsMandatory ? this.MandatoryBackColor : this.BackColor);
}
}