25

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

I've tried this using the code below:

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

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

leanne
  • 7,940
  • 48
  • 77
B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862

3 Answers3

68

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
Ehsan
  • 31,833
  • 6
  • 56
  • 65
3

You can also try the following which might solve you problem:

textBoxResults.SelectAll();

This works well with multi-lined textbox.

Conrad Lotz
  • 8,200
  • 3
  • 23
  • 27
2

This method enables you to select all text within 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();
}

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

Ajay
  • 6,418
  • 18
  • 79
  • 130