I have textBox1 which is read only. I am trying to change
textBox1.ForeColor = Color.Red;
But it does not work. Any idea ?
I have textBox1 which is read only. I am trying to change
textBox1.ForeColor = Color.Red;
But it does not work. Any idea ?
When you set the property of a TextBox
control to ReadOnly
true the text becomes grayed out. That's the default behavior.
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.
Try to cancel the event for 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.
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.
Use a RichTextBox. When it is read only, it continue displaying the text in the color
This should help you.
textboxname.ForeColor = Color.FromKnownColor(KnownColor.selectanycolor);