26

I am writing a dictionary with c# and I'm using a textbox to show the definitions. Obviously, I need to set the ReadOnly property to true, but when I do that I'm unable to change the color of the text. Changing the Font however works fine. What should I do?

I'm using this code and setting the color works perfectly fine when the ReadOnly property is false, but doesn't change when it is true:

    private void button5_Click(object sender, EventArgs e)
    {
        FontDialog fd = new FontDialog();
        fd.ShowColor = true;
        if (fd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            textBox3.Font = fd.Font;
            textBox3.ForeColor = fd.Color;
        }
    }
3
  • 1
    have you tried setting it to .enabled = false? Dec 19, 2013 at 17:51
  • If i disable it the scroll wont work. so there is no use :/
    – Amen
    Dec 19, 2013 at 17:52
  • This is actually documented on MSDN (that the color doesn't change on a ReadOnly control). But the workaround (your accepted answer) is not documented ... except here.
    – davidbak
    Mar 22, 2016 at 23:43

4 Answers 4

73

The ForeColor property of a read-only TextBox is married to the BackColor property for some reason. So if you "tickle" the BackColor property, it will set the ForeColor property after that:

FontDialog fd = new FontDialog();
fd.ShowColor = true;
if (fd.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
  textBox3.Font = fd.Font;
  textBox3.BackColor = textBox3.BackColor;
  textBox3.ForeColor = fd.Color;
}
4
  • 1
    Well all be jiggered. That's a sensible architecture. Ugh. Dec 19, 2013 at 18:53
  • Holy obscurity, Batman! That's non-obvious! (Oddly, this isn't necessary for ResetForeColor.)
    – davidbak
    Mar 22, 2016 at 23:41
  • 1
    @Rajiv Lots and lots of experience where you just end up noticing these things.
    – LarsTech
    Sep 8, 2017 at 13:38
  • Now, that's super weird! Aug 25, 2020 at 9:12
7

Assuming the ForeColor is already set to the desired color (possibly in the designer), all that needs to be done is:

tb.BackColor = tb.BackColor;

This will magically trigger and fix the fore color. Although a comment explaining why this line of code is added is probably also needed.

2
  • Weird, but true in .NET 4.52. Jun 1, 2017 at 17:49
  • Still true in .NET 4.8 Sep 15, 2022 at 15:46
4

In VS 2017 this is not even needed.

In designer if you have set your ForeColor and BackColor as desired and want to switch ReadOnly on your TextBox to True

  • Change BackColor to any random color and compile
  • Change BackColor to your desired color and compile
3

Thanks to LarsTech suggestion I had to set the back color and then set it again

This is the method that worked for me:

    tb.BackColor = Color.Black
    tb.ForeColor = Color.Black
    tb.BackColor = Color.White

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.