In C#, I am creating a form window for a LAN messenger with two textboxes. I need to create a particular textbox as read-only, but any text submitted to it is appearing grey which is not desirable. Is there any way that can be prevented?
5 Answers
I would use a Textbox and set ReadOnly to true, ForeColor to Color.Black, and BackColor to Color.White. This way you can still select the text and copy it with Ctrl-C.

- 3,237
- 1
- 29
- 37
-
Older thread, but I found this useful. The textbox looks normal, except you cannot type into it. Cheers! – Anders Apr 07 '09 at 13:11
You could replace it with a label or on the text box in the KeyPress event, set handled to true:
void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = true;
}

- 37,735
- 14
- 62
- 96
-
3I personally hate this approach because the user still thinks they can edit the text and then gets surprised when the letters they type do nothing. – lc. Feb 26 '09 at 06:06
-
I agree, but it seems that the person asking the question seemed concerned about the grey text in a disabled textbox, this is simpler than inheriting and overriding the paint event. – benPearce Feb 26 '09 at 06:10
-
You can set the colour of the text by setting the Textbox ForeColor property.
For example:
myTextBox.ForeColor = Color.Black

- 59
- 2
In order to keep the textbox white (or Window) when it's read-only, you must explicitly set the BackColor property to Window. To do this, you must first set the BackColor to some other value, then back to Window. The backcolor property should become bold indicating it is no longer the default value.
The grey color is indicative of the ReadOnly state of the textbox. It is a visual indication to the user who will not need to enter text to discover that the textbox is in fact, disabled.
If you need only the readonly behaviour, you would be better off using a Label instead.

- 25,615
- 8
- 56
- 70