简体   繁体   English

如何更改禁用的数字上调的前色/背景色?

[英]How can I change the forecolor / backcolor of a disabled numeric updown?

When I disabled Numeric UpDown control I want that user can still read its value. 当我禁用Numeric UpDown控件时,我希望该用户仍可以读取其值。 But I can't change Forecolor or Backcolor of that tool. 但是我无法更改该工具的前景色或背景色。 I try to use ReadOnly property instead of Enabled/Disabled property but it doesn't worked either. 我尝试使用ReadOnly属性而不是Enabled / Disabled属性,但是它也不起作用。 How can I solve this problem? 我怎么解决这个问题?

There is no way to achieve this goal with the frame work control. 框架控制无法实现此目标。 You can use custom draw to implement it. 您可以使用自定义绘制来实现它。

I just tried via VS2005 and simple WinForms. 我只是通过VS2005和简单的WinForms尝试过。 I put in the EnableChanged event 我放入了EnableChanged事件

private void numericUpDown1_EnabledChanged(object sender, EventArgs e)
{
   NumericUpDown nud = (NumericUpDown)sender;
   nud.BackColor = nud.Enabled ? Color.Yellow : Color.Red;
}

and added another button to the form to just swap its enabled state 并在表单中添加了另一个按钮以仅交换其启用状态

private void button2_Click(object sender, EventArgs e)
{
   this.numericUpDown1.Enabled = ! this.numericUpDown1.Enabled;
}

If you create your own NumericUpDown class derived from the base NumericUpDown class and put it within the that, it will do for all instances of YOUR class used in your app without explicitly doing this color changing in every form. 如果您创建一个从NumericUpDown基类派生的自己的NumericUpDown类,并将其放在其中,则它将对应用程序中使用的YOUR类的所有实例进行处理,而无需显式更改每种形式的颜色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM