user1903439
user1903439

Reputation: 2001

change textcolor of read only text box c#

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

textBox1.ForeColor = Color.Red;

But it does not work. Any idea ?

Upvotes: 1

Views: 23982

Answers (6)

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

Upvotes: 0

Hash_S
Hash_S

Reputation: 79

This should help you.

textboxname.ForeColor = Color.FromKnownColor(KnownColor.selectanycolor);

Upvotes: -1

Manuk
Manuk

Reputation: 1

This should help you:

textBox1.BackColor = Color.FromKnownColor(KnownColor.Control);
textBox1.ForeColor = Color.Red;
textBox1.ReadOnly = true;

Upvotes: 0

dutzu
dutzu

Reputation: 3910

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.

Upvotes: 5

Kai
Kai

Reputation: 2013

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.

Upvotes: 1

Aniket Inge
Aniket Inge

Reputation: 25705

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.

Upvotes: 0

Related Questions