4

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:

enter image description here

But when this field becomes disabled, it keeps a red border around it:

enter image description here

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!!

trailmax
  • 34,305
  • 22
  • 140
  • 234
  • What is the order of setting the red background and disabling the NumericUpdown control? Which one is done first? Disabling the control or setting the background? Try changing the order.. – Sivaraman Dec 04 '12 at 12:51
  • First it is red, then disabled. I'll play with the order of application of colours. – trailmax Dec 04 '12 at 14:18
  • Did you ever get this figured out? I have the same problem with the DomainUpDown control. There is a small line between the border of the textbox control and the domainupdown control that I can't figure out how to color. I also am having trouble coming up with a simple example to show the problem. – CramerTV Sep 24 '13 at 18:47
  • @CramerTV no, never got to fix it. Left it like that. Clients did not seem to mind that glitch. – trailmax Sep 25 '13 at 14:21
  • Glad it worked out for you. My problem was that the control was picking up a color from a web browser I was resizing (smaller) after creating the controls. I ended up hiding the browser before adding controls and even then I had to do the 'Application.DoEvents()' after hiding it before it would stop leaving that line. – CramerTV Sep 25 '13 at 20:07
  • @CramerTV ouch! that sounds like a nasty-nasty bug with no good solution -( – trailmax Sep 26 '13 at 00:08
  • It's a repaint problem. – Xan-Kun Clark-Davis Nov 15 '15 at 10:59

3 Answers3

3

NumericUpdown is a composite of multiple controls. The textbox is inside the NUD and has a one pixel offset. So you are seeing the textbox' BackColor being set differently from the outer NUD control. The true cause of your problem isn't visible in your snippet but a repro for this behavior is:

        numericUpDown1.BackColor = Color.Red;
        numericUpDown1.Enabled = false;
        numericUpDown1.Controls[1].BackColor = SystemColors.InactiveBorder;

You'll need to fix the code that sets the BackColor of the nested control, whatever it looks like. Probably a foreach on the Controls collection.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Hans, thanks for the heads up re composite control - never new that. I've tried what you are suggesting (see the question update with code) and that did not fix the problem. I still have the border around the box -( – trailmax Dec 04 '12 at 15:48
  • I still don't have any idea how to repro your specific problem. Maybe this question will help: http://stackoverflow.com/questions/10631788/numericupdown-backcolor-not-working-as-expected/10634086#10634086 – Hans Passant Dec 04 '12 at 15:58
1

Sorry to bump a question that is nearly 5 years old. IMHO this is a bug in the NUD control. I am currently porting an older Windows CE application to Windows 10. We had no problem with the NUD controls in CE, but now they all are exhibiting the exact same problem trailmax had.

I can add a little bit of observation. The frame of the child control is not being refreshed when the control is disabled. The backcolor has no impact. Whatever is previously displayed on the screen shows on the inside frame of the NUD control. In our application we use several TabPages. Depending on the previously displayed screen we get broken and sometimes colorful frames.

The only work-around I have now is to enable and disable the control after it is displayed on the screen. As I said, our controls are on TabPages, so I am using the tab selected event. Since we have many tabs and many NUD controls, 5 to 20, I loop through all the NUD controls and execute these two lines:

   nudControl.enabled =  !nudControl.enabled
   nudControl.enabled =  !nudControl.enabled

I needn’t check if the control is enabled or not, I simply toggle it to the opposite value, and then toggle it back. In our application this executes very quickly and I do not see any visual flashing on the screen. Again, we have less than 20 NUD’s on any given TabPage.

P.S. I get all the children NUD controls using code from this StackOverflow post: How to get ALL child controls of a Windows Forms form of a specific type (Button/Textbox)?

Joe B
  • 692
  • 8
  • 18
  • Indeed an old question - I don't even have code for this project anymore! Have an upvote for persistence! – trailmax Nov 15 '17 at 13:21
0

I had the same Issue and it turned out, it was just a refresh/repaint problem. The Label was set invalid, but not the whole control, so after forcing a refresh, the border disappeared.

OR just hide and show again :-)

See NumericUpDown backcolor not working as expected

Oh, and I've only seen it with the classic theme (Not that I tried all possible themes, but it surely has to do with the GUI-theme).

Community
  • 1
  • 1
Xan-Kun Clark-Davis
  • 2,664
  • 2
  • 27
  • 38