You can select text programmatically in the Win Forms TextBox control. For example, if you create a function that searches text for a particular string, you can select the text to visually alert the reader of the found string's position.
To select text programmatically
The SelectionStart property is a number that indicates the insertion point within the string of text, with 0 being the left-most position. If the SelectionStart property is set to a value equal to or greater than the number of characters in the text box, the insertion point is placed after the last character.
The SelectionLength property is a numeric value that sets the width of the insertion point. Setting the SelectionLength to a number greater than 0 causes that number of characters to be selected, starting from the current insertion point.
The code below selects the contents of a text box when the control's GotFocus event occurs. The TextBox1_GotFocus event handler must be bound to the control; see Creating Event Handlers at Run Time for Win Forms for more information.
[Visual Basic] Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As EventArgs) TextBox1.SelectionStart = 0 TextBox1.SelectionLength = Len(TextBox1.Text) End Sub [C#]
Controlling the Insertion Point in a TextBox Control | Creating a Password Text Box with the TextBox Control | Creating a Read-Only Text Box | Putting Quotation Marks in a String Programmatically | Viewing Multiple Lines in the TextBox Control| TextBox Control