.net 如何使用 C# 使文本框不可选择
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2800896/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make a textbox non-selectable using C#
提问by saeed
I'm using C#/.NET for a Windows Forms application. I have a text box. How can I make the text box unselectable?
我将 C#/.NET 用于 Windows 窗体应用程序。我有一个文本框。如何使文本框不可选择?
I don't want to disable the complete textbox.
我不想禁用完整的文本框。
回答by Please click here
In the 'Enter' event of the textbox set the ActiveControl to something else:
在文本框的“输入”事件中,将 ActiveControl 设置为其他内容:
private void txtMyTextbox_Enter(object sender, EventArgs e)
{
ActiveControl = objMyOtherControl;
}
回答by Paz
IsHitTestVisible="False"prevents the textbox from displaying the bounding box when hovering or clicking with the mouse. That was enough in my case to take benefit of the textwrapping feature of the textbox while making it insensitive to user actions
IsHitTestVisible="False"防止文本框在鼠标悬停或单击时显示边界框。在我的情况下,这足以利用文本框的文本换行功能,同时使其对用户操作不敏感
回答by Chris Schmich
回答by Bodger
I had the same problem. I had a dialog up and needed multiline text to be displayed. I could not use a label as it has to be a textbox because of the multiline and text wrap.
我有同样的问题。我有一个对话框,需要显示多行文本。由于多行和文本换行,我无法使用标签,因为它必须是一个文本框。
I first mad it readonly, but if the dialog is flashed by other windows, the text becomes selected which looks horrible. So I found the problem.
我首先将其设为只读,但是如果对话框被其他窗口闪烁,则文本会被选中,这看起来很糟糕。所以我发现了问题。
In the form builder program (whatever it is called), there is a property for the TextBox called TabStop. I set that to false and the read-only textbox text nevergets selected. Problem solved.
在表单生成器程序中(不管它叫什么),TextBox 有一个名为 TabStop 的属性。我将其设置为 false,只读文本框文本永远不会被选中。问题解决了。
回答by Loathing
The messages that cause text can be intercepted and blocked. I would have thought that EM_SETSELwould be all that is required. However, even when the mouse clicks and drags, there is no EM_SETELmessage, but the text still selects. Possibly one of the messages has a pointer to a struct that contains the info.
导致文本的消息可以被拦截和阻止。我会认为EM_SETSEL这就是所需要的。但是,即使鼠标单击并拖动,也没有EM_SETEL消息,但文本仍然选择。可能其中一条消息有一个指向包含信息的结构的指针。
Note: I'm not sure if this prevents all ways to select text.
注意:我不确定这是否会阻止所有选择文本的方式。
public class TextBox2 : TextBox {
private const int EM_SETSEL = 0x00B1;
private const int WM_LBUTTONDBLCLK = 0x203;
private const int WM_MOUSEMOVE = 0x200;
private const int WM_KEYDOWN = 0x100;
private const int VK_CONTROL = 0x11;
private const int VK_SHIFT = 0x10;
public TextBox2() {
this.ShortcutsEnabled = false; // disabled right click menu
this.Cursor = Cursors.Default;
}
protected override void OnGotFocus(EventArgs e) {
base.OnGotFocus(e);
HideCaret(this.Handle); // doesn't work from OnHandleCreated
}
[DllImport("user32.dll")]
public static extern bool HideCaret(IntPtr hWnd);
public override bool PreProcessMessage(ref Message msg) {
// prevents the user from using the SHIFT or CTRL + arrow keys to select text
if (msg.Msg == WM_KEYDOWN)
return true;
return base.PreProcessMessage(ref msg);
}
protected override void WndProc(ref Message m) {
if (m.Msg == EM_SETSEL || m.Msg == WM_MOUSEMOVE || m.Msg == WM_LBUTTONDBLCLK)
return;
base.WndProc(ref m);
}
}
回答by NibblyPig
Probably the best way is to put a label behind it, and when you want to make the textbox disabled, hide it and show the label in its place.
可能最好的方法是在它后面放一个标签,当你想禁用文本框时,隐藏它并在它的位置显示标签。
回答by CrazyIvan1974
Old question, and I'm not completely happy with my hack of an answer to this problem, but if the original intention was to use the text box to display data without allowing it to be selected in any way, this is a method that has (so far) worked for me.
老问题,我对我对这个问题的回答并不完全满意,但是如果最初的意图是使用文本框显示数据而不允许以任何方式选择它,那么这是一种方法(到目前为止)对我来说有效。
First, the non-default properties of my text box:
首先,我的文本框的非默认属性:
Multiline = true
ReadOnly = true
ScrollBars = Both
ShortcutsEnabled = false
TabStop = false
WordWrap = false
Then mouse event handling. Both MouseMove and DoubleClick were mapped to trigger textBox1_MouseGeneral(), and I had additional actions happening in the Click event, thus the apparent duplication of the two event handlers.
然后鼠标事件处理。MouseMove 和 DoubleClick 都映射到 trigger textBox1_MouseGeneral(),并且我在 Click 事件中发生了额外的操作,因此两个事件处理程序明显重复。
private void MouseEvent()
{
textBox1.SelectionLength = 0;
focusControl.Focus(); // Move focus to another control
}
private void textBox1_Click(object sender, EventArgs e)
{
MouseEvent();
}
private void textBox1_MouseGeneral(object sender, EventArgs e)
{
MouseEvent();
}
You get a flash of a caret cursor, then textBox1 loses focus.
您会看到一个插入符号光标,然后 textBox1 失去焦点。
回答by Dávid Farkas
private void textBox1_Click(object sender, EventArgs e)
{
this.textBox1.SelectionStart = this.textBox1.Text.Length;
}
And don't forget that: read only=true
并且不要忘记:只读=真
回答by Edglex
@Mystere Man: You might want a text box that cannot be used all the time. For example, I allow the user to create text boxes on a canvas and drag them around. To prevent them from selecting and moving text when they are dragging I need to disallow user input, and text selection also needs to be disabled because it causes a delay which messes up my drag function. In my application the user can only edit a text box when he has double clicked on it, and must then click outside of the text box to be able to move it again.
@Mystere Man:您可能想要一个不能一直使用的文本框。例如,我允许用户在画布上创建文本框并拖动它们。为了防止他们在拖动时选择和移动文本,我需要禁止用户输入,并且还需要禁用文本选择,因为它会导致延迟,从而弄乱了我的拖动功能。在我的应用程序中,用户只能在双击文本框时对其进行编辑,然后必须在文本框外单击才能再次移动它。
I basically have this code (where t is a TextBox):
我基本上有这个代码(其中 t 是一个文本框):
// Prevent text entry
t.IsReadOnly = true;
// Prevent text selection
t.Focusable = false;
This behaviour is preferable to disabling the whole control (t.Enabled = false), since that would also stop mousedown and doubleclick events, which would stop dragging and changing from edit to drag mode from working. Not to mention that the text box would go grey.
此行为比禁用整个控件 ( t.Enabled = false)更可取,因为这也会停止 mousedown 和 doubleclick 事件,这将停止拖动和从编辑模式更改为拖动模式的工作。更不用说文本框会变灰了。
回答by ANDERSON DE LIRA AVELINO
This has worked just fine to me:
这对我来说效果很好:
textBox.ReadOnly = true

