tags:

views:

6400

answers:

4

Hi,

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

Thanks!

+12  A: 

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.

spoon16
It works fine this way as well. Thanks!
niko
If this is your answer go ahead and click the check for me :)
spoon16
It's not an exact answer but it's a fair alternative;)
niko
that's true, feel free to change it to a more exact answer if one pops up.
spoon16
I'll do it if I will continue to look for the exact answer.
niko
+9  A: 

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
Thanks for the info, I was wondering why it wasn't working!Any idea why exactly this is happening?
Soo Wei Tan
A: 

Cheetah - thanks - very helpful....

Who woulda thunk it - that one must set BackColor to actually set the ForeColor ;)

A: 

Unorthodox but effective! ;)