Solved

Set background color of disabled TextBox

Posted on 2010-08-30
5
874 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

759 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

21 Experts available now in Live!

Get 1:1 Help Now