Click here to Skip to main content
Rate this: bad
good
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?
 
Thanx
Posted 3-Apr-12 0:26am
JacoBosch1.5K
Edited 3-Apr-12 0:44am
v3
Rate this: bad
good
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[] {
    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.
  Permalink  
v2
Rate this: bad
good
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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

What I have done is, instead of using Readonly , I use Enabled.
  Permalink  
Rate this: bad
good
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)
        {
            setTextBoxNormal(textBox1);
        }
 
        private void btnSetReadOnly_Click(object sender, EventArgs e)
        {
            setTextBoxReadOnly(textBox1);
        }
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

You might try setting your Textbox enabled to false instead. You can still cange things how you want.
  Permalink  

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



Advertise | Privacy | Mobile
Web03 | 2.8.150302.1 | Last Updated 3 Jan 2015
Copyright © CodeProject, 1999-2015
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