tags:

views:

1460

answers:

5
+1  Q: 

Read-only Textbox

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?

A: 

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.

Cerebrus
+4  A: 

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;
}
benPearce
I 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
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
ReadOnly = true is even easier
spoon16
+2  A: 

You can set the colour of the text by setting the Textbox ForeColor property.

For example:

myTextBox.ForeColor = Color.Black

Michael
+6  A: 

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.

Oran
Older thread, but I found this useful. The textbox looks normal, except you cannot type into it. Cheers!
Anders
A: 

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.