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?
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.
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;
}
You can set the colour of the text by setting the Textbox ForeColor property.
For example:
myTextBox.ForeColor = Color.Black
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.
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.