Share via


Visual Basic Concepts

Working with Selected Text

Text boxes and combo boxes have a series of properties for selected text that are especially useful when working with the Clipboard. These properties, which refer to the block of text selected (highlighted) inside the control, allow you to create cut-and-paste functions for the user. The following properties can all be changed at run time.

Property Description
SelStart A Long integer that specifies the starting position of the selected block of text. If no text is selected, this property specifies the position of the insertion point. A setting of 0 indicates the position just before the first character in the text box or combo box. A setting equal to the length of the text in the text box or combo box indicates the position just after the last character in the control.
SelLength A Long integer that specifies the number of characters selected.
SelText The String containing the selected characters (or an empty string, if no characters are selected).

You can control what text is selected by setting the SelStart and SelLength properties. For example, these statements highlight all the text in a text box:

Text1.SetFocus
' Start highlight before first character.
Text1.SelStart = 0
' Highlight to end of text.
Text1.SelLength = Len(Text1.Text)

If you assign a new string to SelText, that string replaces the selected text, and the insertion point is placed just after the end of the newly inserted text. For example, the following statement replaces the selected text with the string "I’ve just been inserted!":

Text1.SelText = "I’ve just been inserted!"

If no text was selected, the string is simply pasted into the text box at the insertion point.

For More Information   See "SelStart Property," "SelLength Property," and "SelText Property" in the Language Reference.