7

C#中,我正在为带有两个文本框的 LAN Messenger 创建一个表单窗口。我需要创建一个只读的特定文本框,但提交给它的任何文本都显示为灰色,这是不可取的。有什么方法可以预防吗?

4

5 回答 5

23

我会使用文本框并将 ReadOnly 设置为 true,将 ForeColor 设置为 Color.Black,并将 BackColor 设置为 Color.White。这样您仍然可以选择文本并使用 Ctrl-C 复制它。

于 2009-02-26T07:18:34.197 回答
6

您可以将其替换为标签或 KeyPress 事件中的文本框,设置为 true:

void  textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    e.Handled = true;
}
于 2009-02-26T06:00:47.627 回答
2

您可以通过设置Textbox ForeColor属性来设置文本的颜色。

例如:

myTextBox.ForeColor = 颜色.黑色

于 2009-02-26T06:12:08.350 回答
1

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.

于 2009-03-05T23:07:05.293 回答
0

灰色表示文本框的只读状态。它是对不需要输入文本即可发现文本框实际上已禁用的用户的视觉指示。

如果您只需要只读行为,则最好使用 Label 代替。

于 2009-02-26T06:00:38.597 回答