Accessing the selected text

A text selection is text that is highlighted, either by the user or by using the appropriate properties of the control.

You can manage text selection in the Desktop Text Field and Text Area controls using several properties and methods.

The SelectionChanged event is called when the user selects text in the control.

You can use the SelectAll method in your code to select all the text in a control.


These properties can be used to view or change the selected text:




The number of characters currently selected. You can change the selected text by changing this number. Setting this value to 0 (zero) will position the insertion point based on the value in the SelectionStart property rather than selecting any text.


The number of the character just before the selected text. For example, if the fifth character in a Text Field was selected, this property would be 4. Setting this value to 0 (zero) will start the selection at the first character of the Text Field.


A string containing all of the selected text. Changing this value will replace the selected text with the SelectionText value. If no text is selected, the SelectionText value will be inserted at the insertion point (the value in SelectionStart).


Assume you have a Text Area with the text "A quick brown fox."

This code selects "quick":

TextArea1.SelectionStart = 2
TextArea1.SelectionLength = 5

This code changes "quick" to "speedy":

TextArea1.SelectionStart = 2
TextArea1.SelectionLength = 5
TextArea1.SelectedText = "speedy"

This code in the SelectionChanged event change text that the user selects to all uppercase:

Me.SelectedText = Me.SelectedText.Uppercase

See also

Text Field, Text Area topics