Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read-only textbox in C#

Tags:

c#

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?

like image 699
Avik Avatar asked Feb 26 '09 05:02

Avik


2 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.

like image 142
Oran Dennison Avatar answered Oct 01 '22 18:10

Oran Dennison


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;
}
like image 22
benPearce Avatar answered Oct 01 '22 17:10

benPearce