71

Does anyone know which property sets the text color for disabled control? I have to display some text in a disabled TextBox and I want to set its color to black.

intcreator
  • 4,206
  • 4
  • 21
  • 39
Niko Gamulin
  • 66,025
  • 95
  • 221
  • 286

9 Answers9

73

NOTE: see Cheetah's answer below as it identifies a prerequisite to get this solution to work. Setting the BackColor of the TextBox.


I think what you really want to do is enable the TextBox and set the ReadOnly property to true.

It's a bit tricky to change the color of the text in a disabled TextBox. I think you'd probably have to subclass and override the OnPaint event.

ReadOnly though should give you the same result as !Enabled and allow you to maintain control of the color and formatting of the TextBox. I think it will also still support selecting and copying text from the TextBox which is not possible with a disabled TextBox.

Another simple alternative is to use a Label instead of a TextBox.

Eric Schoonover
  • 47,184
  • 49
  • 157
  • 202
59

Additionally, in order for ForeColor to be obeyed on a TextBox marked ReadOnly, you must explicitly set the BackColor. If you want to have it still use the default BackColor, you have to make the set explicit, as the designer is too smart for its own good here. It is sufficient to set the BackColor to its current value. I do this in the Load event for the form, like so:

private void FormFoo_Load(...) {
    txtFoo.BackColor = txtFoo.BackColor;
}
Cheetah
  • 1,141
  • 11
  • 21
  • 5
    IIRC, there is a flag somewhere to check if the colors have been customized. That flag doesn't get set unless the BackColor is set. I can't find it now, but if you rummage around in Reflector, you might be able to. – Cheetah Jan 07 '11 at 16:01
7

I've just found a great way of doing that. In my example I'm using a RichTextBox but it should work with any Control:

public class DisabledRichTextBox : System.Windows.Forms.RichTextBox
{
    // See: http://wiki.winehq.org/List_Of_Windows_Messages

    private const int WM_SETFOCUS   = 0x07;
    private const int WM_ENABLE     = 0x0A;
    private const int WM_SETCURSOR  = 0x20;

    protected override void WndProc(ref System.Windows.Forms.Message m)
    {
        if (!(m.Msg == WM_SETFOCUS || m.Msg == WM_ENABLE || m.Msg == WM_SETCURSOR))
            base.WndProc(ref m);
    }
}

You can safely set Enabled = true and ReadOnly = false, and it will act like a label, preventing focus, user input, cursor change, without being actually disabled.

See if it works for you. Greetings

edoedoedo
  • 1,469
  • 2
  • 21
  • 32
5

hi set the readonly attribute to true from the code side or run time not from the design time

txtFingerPrints.BackColor = System.Drawing.SystemColors.Info;
txtFingerPrints.ReadOnly = true;
syed qaiser
  • 51
  • 1
  • 1
3

You can try this. Override the OnPaint event of the TextBox.

    protected override void OnPaint(PaintEventArgs e)
{
     SolidBrush drawBrush = new SolidBrush(ForeColor); //Use the ForeColor property
     // Draw string to screen.
     e.Graphics.DrawString(Text, Font, drawBrush, 0f,0f); //Use the Font property
}

set the ControlStyles to "UserPaint"

public MyTextBox()//constructor
{
     // This call is required by the Windows.Forms Form Designer.
     this.SetStyle(ControlStyles.UserPaint,true);

     InitializeComponent();

     // TODO: Add any initialization after the InitForm call
}

Refrence

Or you can try this hack

In Enter event set the focus

int index=this.Controls.IndexOf(this.textBox1);

this.Controls[index-1].Focus();

So your control will not focussed and behave like disabled.

Zain Ali
  • 15,535
  • 14
  • 95
  • 108
1

If you want to display text that cannot be edited or selected you can simply use a label

karl f
  • 11
  • 1
1

In addition to the answer by @spoon16 and @Cheetah, I always set the tabstop property to False on the textbox to prevent the text from being selected by default.

Alternatively, you can also do something like this:

private void FormFoo_Load(...) {
    txtFoo.Select(0, 0);
}

or

private void FormFoo_Load(...) {
    txtFoo.SelectionLength = 0;
}
Johnie Karr
  • 2,744
  • 2
  • 35
  • 44
1

Just handle Enable changed and set it to the color you need

private void TextBoxName_EnabledChanged(System.Object sender, System.EventArgs e)
{
    ((TextBox)sender).ForeColor = Color.Black;
}
AeroX
  • 3,387
  • 2
  • 25
  • 39
Mahmoud Salah
  • 144
  • 1
  • 5
1

Setting the 'Read Only' as 'True' is the easiest method.

IT_Boy
  • 23
  • 4