?
Solved

Set background color of disabled TextBox

Posted on 2010-08-30
5
Medium Priority
?
922 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
Suggested Courses

801 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