Improve company productivity with a Business Account.Sign Up

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1081
  • Last Modified:

Set background color of disabled TextBox


I have a specific set of conditions where it would be great if I could change the ForeColor of a disabled TextBox from it's default gray to a different color (Black). Does anyone know how to accomplish this?

i have already tried to do it via a simple bit of code. I imagined that this would not work and of course my expectations were correct.

if(TextBox1.Enabled == false)
  TextBox1.ForeColor = System.Drawinig.Color.Black;

Thanks in advance.
  • 3
1 Solution
- Try this TextBox1.BackColor = System.Drawinig.Color.Black;
There is no way to do this (easily) using the built-in TextBox properties.

However, there are several alternative options. I can provide more information on whichever one you are interested in.

1) Instead of disabling the textbox, just set it to ReadOnly = True. That way it will have whatever color you want, but it won't allow the user to change the contents.

2) When an textbox is disabled, make it invisible and simply show a Label in its place instead (with the same Text). Then you can set the colors of the label as you wish and it will not allow the user to interact with it in any way.

3) Create a user control with a public property to enable or disable the control. Then you could have a text property to set a label and text box simultaneously. The basic idea is that the two controls would switch visability depending on if the control was enabled. This way it would not require any code changes in the rest of your application. Just use the user-control instead of a Textbox.

4) The most complicated method (although it is still fairly simple to do) is to create a new class that inherits from TextBox (i.e. a subclass). You can override the OnPaint method and simply use the Graphics.DrawString method to draw the text however you want, and in whatever color you want when the TextBox is disabled. The code would be something like the following:

include all the normal Using statements
//using ....
using System.Windows.Forms;

public class MyTextBox : TextBox {
public MyTextBox() {
     //Note: You need to set the ControlStyles to "UserPaint" in the constructor.

protected override void OnPaint(PaintEventArgs e)
    if (this.Enabled) 
      base.OnPaint(e);  //use the standard paint routine
    else { //use the alternate color if disabled
     SolidBrush drawBrush = new SolidBrush(ForeColor); //Use whatever color you want
     // Draw string to screen.
     e.Graphics.DrawString(Text, Font, drawBrush, 0f,0f); //Use the Font property

Open in new window

Note: your post says "Background color". If you really do want the background color to change, that is easy, and you can set it directly using the BackColor property as thaytu88 pointed out. But I assume you really meant the ForeColor, which is what my post was about.

For more info, please see the following references:
tatton777Author Commented:
Ricovox supplied several well explained approaches to my problem. I appreciated the time that he spent providing these different options. Not only did his explanations supply me with the answer that I needed but it also provided me with different ways of thinking about the issue.  I especially appreciated the explanation on the creation of a sub-class.

Ricovox is a an excellent Expert indeed.
Thanks for the great comment, tatton777. I'm glad I could help.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now