[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Set background color of disabled TextBox

Posted on 2010-08-30
5
Medium Priority
?
937 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

649 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