Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Good day

I made my textboxes reaadonly and I want to change the forecolor of the text inside of the textboxes.

I have written the following code, but it is not changing the forecolor of the text inside of the textbox to grey.

txtVersion.ForeColor = Color.Gray;


How can I change the forecolor of the text, when the textbox is made readonly?

Thanx
Posted
Updated 23-Mar-20 10:56am
v3

There is something odd about the ForeColor property of a read only System.Windows.Form.TextBox which I haven't seen documented, although I haven't looked very hard.

I've noticed that a foreground colour change only takes effect after the background colour has been changed once. After that any subsequent foreground changes take place immediately.

My little test app:
C#
using System;
using System.Drawing;
using System.Windows.Forms;

// Requires a form with 2 text boxes and 2 buttons

public partial class Form1 : Form {
  private readonly Color[] rainbow = new Color[] {
    Color.Red,
    Color.Orange,
    Color.Yellow,
    Color.Green,
    Color.Blue,
    Color.Indigo,
    Color.Violet
  };

  private Int32 foreIdx, backIdx;

  public Form1() {
    InitializeComponent();
    backIdx = rainbow.Length - 1;
    NormalTextBox.Text = "Standard text box";
    ReadOnlyTextBox.Text = "Read only text box";
  }

  private void ForeColBtn_Click(object sender, EventArgs e) {
    NormalTextBox.ForeColor = rainbow[foreIdx];
    ReadOnlyTextBox.ForeColor = rainbow[foreIdx];
    foreIdx = ++foreIdx % rainbow.Length;
  }

  private void BackColBtn_Click(object sender, EventArgs e) {
    NormalTextBox.BackColor = rainbow[backIdx];
    ReadOnlyTextBox.BackColor = rainbow[backIdx];
    backIdx = ++backIdx % rainbow.Length;
  }
}


Have fun,

Alan.
 
Share this answer
 
v2
Comments
Member 11391309 25-Mar-15 0:24am    
dsf
You can't, without a lot of work.

When you set the textbox to readonly, the foreground and background colours are overridden and ignored in favour of the system disabled colors - and it is a BAAAAD idea to change them.

The easiest way to do it would be to create your own control, derived from TextBox and handle ReadOnly yourself - disable text entry via the KeyPress event and set the color yourself.
 
Share this answer
 
What I have done is, instead of using Readonly , I use Enabled.
 
Share this answer
 
Setting the BackColor property to any color before you set the ReadOnly property of a textbox to true solves the problem:

C#
private void setTextBoxReadOnly(TextBox txtBoxToChange)
{
    txtBoxToChange.BackColor = Color.Gray;
    txtBoxToChange.ForeColor = Color.Red;
    txtBoxToChange.ReadOnly = true;
}

private void setTextBoxNormal(TextBox txtBoxToChange)
{
    txtBoxToChange.BackColor = Color.White;
    txtBoxToChange.ForeColor = Color.Black;
    txtBoxToChange.ReadOnly = false;
}

private void btnNormal_Click(object sender, EventArgs e)
{
    setTextBoxNormal(textBox1);
}

private void btnSetReadOnly_Click(object sender, EventArgs e)
{
    setTextBoxReadOnly(textBox1);
}


Alternatively, use this reusable static method.

C#
private static void ChangeReadOnlyTextBoxColor (
            TextBox ptxtChangeThis ,
            Color pclrNewForeColor )
        {
            Color clrBackColorOriginal = ptxtChangeThis.BackColor;
            ptxtChangeThis.BackColor = Color.Black;
            Application.DoEvents ( );
            ptxtChangeThis.BackColor = clrBackColorOriginal;
            Application.DoEvents ( );
            ptxtChangeThis.ForeColor = pclrNewForeColor;
            Application.DoEvents ( );
        }   // ChangeReadOnlyTextBoxColor
 
Share this answer
 
v3
You might try setting your Textbox enabled to false instead. You can still cange things how you want.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900