Read-only textbox in C#

2019-01-28 00:53发布

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?

标签: c# textbox
5条回答
迷人小祖宗
2楼-- · 2019-01-28 01:36

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楼-- · 2019-01-28 01:47

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

For example:

myTextBox.ForeColor = Color.Black

查看更多
看我几分像从前
4楼-- · 2019-01-28 01:49

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;
}
查看更多
倾城 Initia
5楼-- · 2019-01-28 01:51

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.

查看更多
我命由我不由天
6楼-- · 2019-01-28 01:53

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.

查看更多
登录 后发表回答