更改只读文本框的文本颜色 c#
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14983341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
change textcolor of read only text box c#
提问by user1903439
I have textBox1 which is read only. I am trying to change
我有只读的 textBox1。我正在努力改变
textBox1.ForeColor = Color.Red;
But it does not work. Any idea ?
但它不起作用。任何的想法 ?
回答by dutzu
When you set the property of a TextBox
control to ReadOnly
true the text becomes grayed out. That's the default behavior.
当您将TextBox
控件的属性设置为ReadOnly
true 时,文本会变灰。这是默认行为。
If you have a requirement to show it in Red, then you shouldn't set the ReadOnly
property but rather handle the TextChanged
events manually and keep the old value intact. But i don't recommend it.
如果您需要以红色显示它,那么您不应该设置ReadOnly
属性,而是TextChanged
手动处理事件并保持旧值不变。但我不推荐它。
回答by Aniket Inge
what you can do to a read-only textbox is (first change it to read/write) you can override the KeyPress()
event of the said TextBox
and ignore all the inputs from there onwards.
您可以对只读文本框执行的操作是(首先将其更改为读/写)您可以覆盖所述KeyPress()
事件TextBox
并忽略从那里开始的所有输入。
回答by Kai
Try to cancel the event for KeyPress:
尝试取消 KeyPress 的事件:
textBox1.Text = "Test";
textBox1.ForeColor = Color.Red;
textBox1.KeyPress += new KeyPressEventHandler(textBox1_KeyPress);
void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = true;
}
ReadOnly property always greyed the control out. This is default behaviour.
ReadOnly 属性总是使控件变灰。这是默认行为。
回答by Manuk
This should help you:
这应该可以帮助您:
textBox1.BackColor = Color.FromKnownColor(KnownColor.Control);
textBox1.ForeColor = Color.Red;
textBox1.ReadOnly = true;
回答by Hash_S
This should help you.
这应该对你有帮助。
textboxname.ForeColor = Color.FromKnownColor(KnownColor.selectanycolor);