trailmax
trailmax

Reputation: 35106

NumericUpDown background colour change for disabled element

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

Upvotes: 4

Views: 2902

Answers (3)

Joe B
Joe B

Reputation: 760

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)?

Upvotes: 1

Xan-Kun Clark-Davis
Xan-Kun Clark-Davis

Reputation: 2843

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).

Upvotes: 0

Hans Passant
Hans Passant

Reputation: 941455

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.

Upvotes: 3

Related Questions