更改只读文本框的文本颜色 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:40:00  来源:igfitidea点击:

change textcolor of read only text box c#

c#textboxreadonly

提问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 TextBoxcontrol to ReadOnlytrue the text becomes grayed out. That's the default behavior.

当您将TextBox控件的属性设置为ReadOnlytrue 时,文本会变灰。这是默认行为。

If you have a requirement to show it in Red, then you shouldn't set the ReadOnlyproperty but rather handle the TextChangedevents 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 TextBoxand 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);