NumericUpDown禁用元素的背景颜色更改 [英] NumericUpDown background colour change for disabled element

查看:270
本文介绍了NumericUpDown禁用元素的背景颜色更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的winform应用程序上,我试图在必填字段上进行颜色编码.在用户编辑时,当填写了所需的输入时,背景变为浅绿色,如果必填字段为空,则背景为红色. 根据其他字段中的输入,某些字段是启用还是禁用的,因此有时我需要将其禁用的字段,并且应将其完全禁用(禁用的彩色背景). 这就是我要进行的背景更改:

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;
}

类似的功能可在TextBox上使用,并且在没有毛刺的情况下也可以正常工作. 但是NumericUpDown行为异常. 这是我在必填字段为空时看到的内容:

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:根据Han的回答,我迅速更新了代码,但这仍然行不通.

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);
        }
    }

我大致像这样使用它:

numericUpDown1.Enabled = true;
numericUpDown1.SetBackgroundColor(Color.Red);
numericUpDown1.Enabled = false;
numericUpDown1.SetBackgroundColor(SystemColors.InactiveBorder);

尽管我遍历了NUD的所有子控件并在其中更改了背景色,但仍然在文本框周围获得了该框架.还有其他想法吗?

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?

干杯!

推荐答案

NumericUpdown是多个控件的组合.文本框位于NUD内部,偏移量为1像素.因此,您会看到文本框的BackColor设置与外部NUD控件不同.您的问题的真正原因在您的代码段中不可见,但是对此行为的再现是:

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;

无论外观如何,您都需要修复设置嵌套控件的BackColor的代码.可能是有关Controls集合的前言.

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.

这篇关于NumericUpDown禁用元素的背景颜色更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆