0

I have textBox1 which is read only. I am trying to change

textBox1.ForeColor = Color.Red;

But it does not work. Any idea ?

user1903439
  • 1,951
  • 7
  • 19
  • 29
  • Found this question http://stackoverflow.com/questions/276179/how-to-change-the-font-color-of-a-disabled-textbox , might be what you are looking for. – Roise Feb 20 '13 at 15:21

6 Answers6

4

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.

Sufian
  • 6,405
  • 16
  • 66
  • 120
dutzu
  • 3,883
  • 13
  • 19
  • @BrianSnow - Well, because it tends to get messy if you start working in the code-behind rather than having your UI encapsulated and loosely coupled to the logic behind. – dutzu Apr 20 '15 at 08:05
  • A better approach would be to have the texbox with the behavior you want as a custom control (in case you intend on reusing it) – dutzu Apr 20 '15 at 08:11
  • 2
    A simpler solution is just to set the textbox BackColor to itself. that is textBox1.BackColor = textBox1.BackColor; textBox1.ForeColor = Color.Red; textBox1.ReadOnly = true; This sets the colors to custom – davehay Jul 13 '16 at 09:07
1

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.

Kai
  • 1,953
  • 2
  • 13
  • 18
0

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.

Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
0

This should help you:

textBox1.BackColor = Color.FromKnownColor(KnownColor.Control);
textBox1.ForeColor = Color.Red;
textBox1.ReadOnly = true;
Sufian
  • 6,405
  • 16
  • 66
  • 120
Manuk
  • 1
0

Use a RichTextBox. When it is read only, it continue displaying the text in the color

-1

This should help you.

textboxname.ForeColor = Color.FromKnownColor(KnownColor.selectanycolor);
Hash_S
  • 79
  • 4
  • 18
  • Hi there, welcome to SO. Please see our help centre, particularly http://stackoverflow.com/help/how-to-answer. In particular, could you please edit your answer to explain how and why this solves the asker's problem? – Tim Malone Jun 05 '16 at 06:28