How can I select all the text in a Windows Forms text box?

I want to select all the text that is in the text box.

I tried this using the following code:

textBoxResults.SelectionStart = 0; textBoxResults.SelectionLength = textBoxResults.Text.Length; 

Source: I got this code here http://msdn.microsoft.com/en-us/library/vstudio/hk09zy8f(v=vs.100).aspx but for some reason it doesn't seem to work.

+9
c # winforms textbox
Aug 05 '13 at 4:44 on
source share
3 answers

You can use the built-in method for this purpose.

 textBoxResults.SelectAll(); textBoxResults.Focus(); //you need to call this to show selection if it doesn't has focus 
+36
Aug 05 '13 at 4:48
source share

You can also try the following which can solve your problem:

 textBoxResults.SelectAll(); 

This works well with a multi-line text box.

+1
Aug 05 '13 at 4:53
source share

This method allows you to select all the text inside the control.

 public void CopyAllMyText() { // Determine if any text is selected in the TextBox control. if(textBox1.SelectionLength == 0) // Select all text in the text box. textBox1.SelectAll(); // Copy the contents of the control to the Clipboard. textBox1.Copy(); } 

See this link for more information. http://msdn.microsoft.com/en-us/library/system.windows.forms.textboxbase.selectall.aspx

+1
Aug 05 '13 at 5:01
source share



All Articles