简体   繁体   English

C#当我双击时如何选择文本框中的所有文本?

[英]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.

相关问题 当用户单击所有选定的文本时,如何创建按钮必须在 c# 中的富文本框的中心对齐 - How can I create a button when a user click all selected text must be align on the center of rich textbox in c# 当我双击文本框中的一个单词时,我想在C#wpf中获得该单词(“ text”) - When I double click on a word in the textbox I want to get that word(“text”) in a C# wpf 如何在c#中将文本框值转换为double? - How can I convert textbox value to double in c#? 在C#单击按钮后,如何从DataGridView等于textbox.Text中选择一行? - How can I select a row from the DataGridView equals textbox.Text after clicking a button by C# ? 如何选择 Windows 窗体文本框中的所有文本? - How can I select all the text within a Windows Forms textbox? 单击C#文本框中的一行时,如何选择整行/整行 - How can i select the whole row/line when clicking on a line in a textbox in C# 如何将文本从表单返回到文本框C#? - How can I return text from a form to a textbox C#? 如何让文本框文本公开 C# forms - How can I get a textbox text to be public C# forms 在C#WPF中的DataGrid中双击特定单元格时,如何弹出消息框或文本框 - How can I make a messagebox or textbox pop up when a specific cell is double clicked in my DataGrid in C# WPF 当我单击 C# Windows 表单中的按钮时,如何从来自文本框的另一个 class 中获取值? - how can I reach the value from another class coming from textbox when I click the button in C# Windows form?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM