How do you change the text color of a readonly TextBox?

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

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;
}

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

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.

Tags:

C#