阻止在 C# Windows 窗体中突出显示文本框中的文本的功能
我想用 C# 创建一个带有文本框的 Windows 窗体,其文本不能由用户更改或选择。 我设法使其不可更改 (ReadOnly = true
),但我仍然能够选择并突出显示文本。
我怎样才能改变这个?
谢谢!
-R
I want to create a Windows form in C# with textbox whose text cannot be altered or selected by the user.
I managed to make it unalterable (ReadOnly = true
), but I am still able to select and highlight the text.
How can I change this?
Thanks!
-R
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是预期行为,也是设计使然。如果您右键单击 Windows 资源管理器中的某个项目并打开其“属性”窗口,您可以亲自看到这一点。 “常规”选项卡上的所有动态文本字段都是可选择的,但不可修改。 如果这不是您想要的行为,您应该强烈考虑使用
Label
控件。这正是其预期的情况 - 用户无法选择的静态文本。如果出于某种原因,您不明白为什么应该使用
Label
控件,我将继续回答您提出的问题。 尝试覆盖此行为的主要问题(例如覆盖任何控件的默认行为)是有很多用户完成此操作的方法: 用鼠标单击并拖动、双击鼠标按钮、使用键盘上的箭头键与 Shift 键结合使用、按 Tab 键进入文本框、右键单击并从上下文菜单中选择“全选”等...
您的第一反应可能是覆盖 .NET Framework 公开的潜在相关事件(例如
OnMouseDown
、OnKeyDown
等)并将所选长度重置为 0,以便文本在自动选中后立即取消选中。然而,这会在过程中产生一点闪烁效果,这可能对您来说可以接受,也可能不可以接受。或者,您可以通过重写控件的 WndProc 方法、监视相关窗口消息并阻止消息完全传递到控件的基类来执行相同的操作。这将防止闪烁效果,因为基本
TextBox
控件实际上从未收到导致其自动选择文本的消息,但它确实具有明显的副作用,即阻止控件因此采取任何操作这些共同事件。在这种情况下,这对你来说似乎并不重要,但要意识到这仍然是一个相当激烈的黑客攻击。我绝对不能推荐它作为良好的做法。话虽这么说,如果您仍然感兴趣,可以使用以下自定义
UnselectableTextBox
类。它阻止处理我能想到的可能允许用户选择文本的每个窗口消息。它确实有效,但公平警告:可能还有其他我没有想到的。This is the expected behavior and is by design. You can see this for yourself if you right-click on an item in Windows Explorer and open its Properties window. All of the dynamic text fields on the "General" tab are selectable, though not modifiable. If this is not the behavior you desire, you should strongly consider using a
Label
control instead. This is the exact situation for which it was intended—static text that is not selectable by the user.In case, for whatever reason, you're not sold on why you should use a
Label
control instead, I'll proceed onward in answering the question you asked. The major problem with trying to override this behavior (like overriding the default behavior of any control) is that there are a lot of ways for the user to accomplish it: clicking and dragging with the mouse, double-clicking the mouse button, using the arrow keys on the keyboard in combination with the Shift key, tabbing into the textbox,right-clicking and choosing "Select All" from the context menu, etc...
Your first instinct might be to override the potentially relevant events exposed by the .NET Framework (such as
OnMouseDown
,OnKeyDown
, etc.) and reset the selected length to 0 so that the text is immediately unselected after it is automatically selected. However, this causes a little bit of a flicker effect in the process, which may or may not be acceptable to you.Alternatively, you could do the same thing by overriding the control's
WndProc
method, watching for the relevant window messages, and preventing the messages from being passed on to the control's base class altogether. This will prevent the flicker effect because the baseTextBox
control never actually receives a message causing it to automatically select its text, but it does have the obvious side effect of preventing the control from taking any action as a result of these common events. It seems like that probably doesn't matter to you in this case, but realize that this is still a pretty drastic hack. I definitely cannot recommend it as being good practice.That being said, if you're still interested, you could use the following custom
UnselectableTextBox
class. It prevents the processing of every window message that I could think of which would possibly allow the user to select text. It does indeed work, but fair warning: there may be yet others I haven't thought of.使用
标签
代替文本框
。我认为你只想显示信息,但不想选择,不想复制,使用label
将是更好的选择......Use
label
inplace oftextbox
. I think you only want to show information but not to select, not to copy, usinglabel
will be better option...禁用它是一种选择吗? (启用=假)
Is disabling it an option? (Enabled = false)