[英]Setting a ReadOnly TextBox default BackColor
I have a TextBox
which is set to be ReadOnly
.我有一个设置为
ReadOnly
的TextBox
。
At some point that TextBox
is being available for editing, and it's BackColor
changes (It is indicating if the value is valid).在某些时候
TextBox
可用于编辑,并且它的BackColor
更改(它指示该值是否有效)。
If I want to set the TexBox
back to ReadOnly
, the TextBox
doesn't get back the original BackColor
that a ReadOnly
TextBox
gets.如果我想将
TexBox
设置回ReadOnly
,则TextBox
不会取回ReadOnly
TextBox
获得的原始BackColor
。
What should I do in order to get the original color again?我该怎么做才能再次获得原始颜色?
I realize I can set the color manually to SystemColors.Control
, but is this the "right way"?我意识到我可以手动将颜色设置为
SystemColors.Control
,但这是“正确的方式”吗?
This is a simple code for demonstration.这是一个简单的演示代码。 If
SystemColors.Control
is the way to go, I will change it in the ReadOnlyChanged
event...如果
SystemColors.Control
是SystemColors.Control
的方法,我将在ReadOnlyChanged
事件中更改它...
private void button1_Click(object sender, EventArgs e)
{
//At this point this.textBox1 is ReadOnly
this.textBox1.ReadOnly = false;
this.textBox1.BackColor = Color.Orange;
/*this.textBox1.BackColor = SystemColors.Control;*/ //Is this the right way?
this.textBox1.ReadOnly = true; //Textbox remains orange...
}
You have to set BackColor
to the look of a ReadOnly TextBox's BackColor
, that is Color.FromKnownColor(KnownColor.Control)
:您必须将
BackColor
设置为ReadOnly TextBox's BackColor
的外观,即Color.FromKnownColor(KnownColor.Control)
:
//this is the ReadOnlyChanged event handler for your textbox
private void textBox1_ReadOnlyChanged(object sender, EventArgs e){
if(textBox1.ReadOnly) textBox1.BackColor = Color.FromKnownColor(KnownColor.Control);
}
You may need a variable to store the current BackColor every time your TextBox's BackColor changes:每次 TextBox 的 BackColor 更改时,您可能需要一个变量来存储当前的 BackColor:
Color currentBackColor;
bool suppressBackColorChanged;
private void textBox1_BackColorChanged(object sender,EventArgs e){
if(suppressBackColorChanged) return;
currentBackColor = textBox1.BackColor;
}
private void textBox1_ReadOnlyChanged(object sender, EventArgs e){
suppressBackColorChanged = true;
textBox1.BackColor = textBox1.ReadOnly ? Color.FromKnownColor(KnownColor.Control) : currentBackColor;
suppressBackColorChanged = false;
}
Yes, that's fine.是的,没关系。 There's no reason you can't use the SystemColors to specify the desired color for the control.
没有理由不能使用 SystemColors 为控件指定所需的颜色。 I've never heard of anything in
WinForms
that would cause a control to automatically revert to its default color upon setting ReadOnly = true
.我从未听说过
WinForms
中的任何内容会导致控件在设置ReadOnly = true
时自动恢复为其默认颜色。
I suppose one alternative is to create a class-level variable called textBox1OriginalColor
or something and set it in the form's Load
event.我想一种替代方法是创建一个名为
textBox1OriginalColor
或其他东西的类级变量,并将其设置在表单的Load
事件中。 Then you know exactly what it was when the form was originally displayed, if you think someone might in the future set the text box's default background color to, say, blue in the designer or something.然后,如果您认为将来有人可能会在设计器中将文本框的默认背景颜色设置为蓝色或其他颜色,那么您就可以确切地知道表单最初显示时是什么颜色。
I know this is an old question, but for posterity sake:我知道这是一个老问题,但为了后代:
TextBox as well as many other controls rely on Color.Empty to decide whether or not to display its default color. TextBox 以及许多其他控件依赖 Color.Empty 来决定是否显示其默认颜色。
To set a TextBox back to the system default (irregardless of state):要将 TextBox 设置回系统默认值(无论状态如何):
textBox1.BackColor = Color.Empty;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.