How can I select all the text within a Windows Forms textbox?

B. Clay Shannon picture B. Clay Shannon · Aug 5, 2013 · Viewed 50.6k times · Source

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.

Answer

Ehsan picture Ehsan · Aug 5, 2013

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