Search code examples
c#winformstextboxselectall

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


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.


Solution

  • 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