开发者

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? 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开发者_如何学Go


TextBox tb = new TextBox();
tb.SelectAll();

The TextBox has a SelectAll method which you can use. Add it in your double click event handler.


try something like this. When the MouseDoubleClick-Event is fired...

myTextBox.SelectAll();

Just check the 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.


You can attach a DoubleClick event handler to the textbox and then call the SelectAll method


Assuming we are talking WindowsForms, then all you have to do is attach an EventHandler to the DoubleClick event and invoke SelectAll

private void sampleTextBox_DoubleClick(object sender, EventArgs e)
{
  sampleTextBox.SelectAll();
}


The textbox control exposes the SelectionStart and Selection Length properties.

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).

On edit: The above solution to use SelectAll() is much easier.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜