Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: C# C#4.0 Color WinForm
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?
Posted 2-Apr-12 23:26pm
Edited 2-Apr-12 23:44pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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:
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[] {
  private Int32 foreIdx, backIdx;
  public Form1() {
    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,
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

Setting the BackColor property to any color before you set the ReadOnly property of a textbox to true solves the problem:
        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)
        private void btnSetReadOnly_Click(object sender, EventArgs e)
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

What I have done is, instead of using Readonly , I use Enabled.

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

  Print Answers RSS
0 OriginalGriff 225
1 Richard MacCutchan 100
2 kbrandwijk 90
3 ChauhanAjay 80
4 ProgramFOX 80
0 Sergey Alexandrovich Kryukov 9,050
1 OriginalGriff 8,151
2 CPallini 2,613
3 Richard MacCutchan 2,221
4 Abhinav S 1,928

Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 17 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100