Solved

Set background color of disabled TextBox

Posted on 2010-08-30
5
890 Views
Last Modified: 2013-11-27
Hello.

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.
0
Comment
Question by:tatton777
  • 3
5 Comments
 
LVL 6

Expert Comment

by:thaytu888888
ID: 33560223
- Try this TextBox1.BackColor = System.Drawinig.Color.Black;
0
 
LVL 4

Accepted Solution

by:
ricovox earned 500 total points
ID: 33560507
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.
     this.SetStyle(ControlStyles.UserPaint,true);
}

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

0
 
LVL 4

Expert Comment

by:ricovox
ID: 33560538
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:

http://social.msdn.microsoft.com/forums/en-US/winforms/thread/de63a4e2-f453-49b3-ab0a-c338080681f0/

http://bytes.com/topic/c-sharp/answers/265646-changing-textbox-backcolor-forecolor-when-disabled

http://www.eggheadcafe.com/software/aspnet/31934749/change-color-of-disabled-textbox-text.aspx
0
 
LVL 1

Author Closing Comment

by:tatton777
ID: 33560796
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.
0
 
LVL 4

Expert Comment

by:ricovox
ID: 33567093
Thanks for the great comment, tatton777. I'm glad I could help.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now