On my winform application I'm trying to do colour-coding on the required fields. On user editing, when a required input is filled in, the background becomes light green, if required field is empty, it's background is red. Some fields are enabled and disabled depending on the input in other fields, so sometimes I have required field that is disabled, and that should be completely disabled (disabled colour background). This is what I have for the background change:
public static void UpdateBackgroundColor(this NumericUpDown control)
{
if (!control.Enabled)
{
control.BackColor = SystemColors.InactiveBorder;
return;
}
var inputValue = control.Value;
if (inputValue == 0)
{
control.BackColor = Color.Red;
return;
}
control.BackColor = Color.LightGreen;
}
Similar function works on TextBox and works fine with no glitches. But NumericUpDown is misbehaving. This is what I see when the field is required and empty:
But when this field becomes disabled, it keeps a red border around it:
The same story happens when background is green and becomes disabled.
So why does this happen and how to fix it?
UPD: As per Han's answer, I quickly updated my code, but that still does not work.
private static void SetBackgroundColor(this Control control, Color color)
{
control.BackColor = color;
foreach (Control childControl in control.Controls)
{
SetBackgroundColor(childControl, color);
}
}
And I'm roughly using it like this:
numericUpDown1.Enabled = true;
numericUpDown1.SetBackgroundColor(Color.Red);
numericUpDown1.Enabled = false;
numericUpDown1.SetBackgroundColor(SystemColors.InactiveBorder);
And still get that frame around the text box, despite the fact that I go through all the child controls of NUD and change the back colours there. Any other ideas?
Cheers!!