C# 中的只读文本框
在 C# 中,我正在为带有两个文本框的 LAN Messenger 创建一个表单窗口。 我需要创建一个特定的文本框作为只读,但是提交给它的任何文本都显示为灰色,这是不可取的。 有什么办法可以预防吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我将使用文本框并将 ReadOnly 设置为 true,将 ForeColor 设置为 Color.Black,将 BackColor 设置为 Color.White。 这样您仍然可以选择文本并使用 Ctrl-C 复制它。
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.
您可以将其替换为标签或在 KeyPress 事件中的文本框中将处理设置为 true:
You could replace it with a label or on the text box in the KeyPress event, set handled to true:
您可以通过设置 Textbox ForeColor 属性来设置文本的颜色。
例如:
myTextBox.ForeColor = Color.Black
You can set the colour of the text by setting the Textbox ForeColor property.
For example:
myTextBox.ForeColor = Color.Black
为了使文本框在只读时保持白色(或窗口),必须将 BackColor 属性显式设置为窗口。 为此,您必须首先将 BackColor 设置为其他值,然后再设置回 Window。 背景色属性应变为粗体,表明它不再是默认值。
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.
灰色表示文本框的只读状态。 这是向用户提供的视觉指示,用户无需输入文本即可发现文本框实际上已被禁用。
如果您只需要只读行为,那么最好使用标签。
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.