[英]C# how can I select all the text in a textbox when I double click?
Using C# how can I select all the text in a textbox when I double click? 使用C#,当我双击时如何选择文本框中的所有文本? My text contains spaces "This is a test", when I double click by default only one word is highlighted, how can I highlight all the text?
我的文字包含空格“这是一个测试”,当我双击默认情况下只突出显示一个单词时,如何突出显示所有文字?
What I am trying to achieve is a quick way for users to clear the texbox of text, the text exceeds the length of the box so you can't select the end and drag back to delete, you have to click and use the backspace and delete keys to clear the text. 我想要实现的是用户快速清除文本的文本框,文本超出框的长度,因此您无法选择结束并拖回删除,您必须单击并使用退格键和删除键以清除文本。
Thanks Alison 谢谢艾莉森
TextBox tb = new TextBox();
tb.SelectAll();
The TextBox has a SelectAll
method which you can use. TextBox有一个可以使用的
SelectAll
方法。 Add it in your double click event handler. 在双击事件处理程序中添加它。
try something like this. 尝试这样的事情。 When the MouseDoubleClick-Event is fired...
当MouseDoubleClick-Event被触发时......
myTextBox.SelectAll();
Just check the MSDN --> http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.textboxbase.selectall.aspx 只需查看MSDN - > http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.textboxbase.selectall.aspx
Triple clicks could select the whole paragraph. 三次点击可以选择整个段落。 If you change the behavior of double click, word selection could be a little hard.
如果您更改双击的行为,单词选择可能会有点困难。
您可以将DoubleClick事件处理程序附加到文本框,然后调用SelectAll方法
Assuming we are talking WindowsForms, then all you have to do is attach an EventHandler to the DoubleClick event and invoke SelectAll 假设我们正在讨论WindowsForms,那么您所要做的就是将一个EventHandler附加到DoubleClick事件并调用SelectAll
private void sampleTextBox_DoubleClick(object sender, EventArgs e)
{
sampleTextBox.SelectAll();
}
The textbox control exposes the SelectionStart and Selection Length properties. 文本框控件公开SelectionStart和Selection Length属性。
You just need to simply wire the double click event of the textbox to set those properties. 您只需简单地连接文本框的双击事件即可设置这些属性。
SelectionStart will be 0. SelectionLength will be the length of the text (easily determined by the Text property). SelectionStart将为0. SelectionLength将是文本的长度(很容易由Text属性确定)。
On edit: The above solution to use SelectAll() is much easier. 在编辑时:使用SelectAll()的上述解决方案要容易得多。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.