Export (0) Print
Expand All
Expand Minimize

SelLength, SelStart, SelText Properties (ActiveX Controls)

Visual Studio .NET 2003

SelLength — returns or sets the number of characters selected.

SelStart — returns or sets the starting point of text selected; indicates the position of the insertion point if no text is selected.

SelText — returns or sets the string containing the currently selected text; consists of a zero-length string ("") if no characters are selected.

These properties aren't available at design time.

Syntax

object.SelLength [= number]

object.SelStart [= index]

object.SelText [= value]

The SelLength, SelStart, and SelText property syntaxes have these parts:

Part Description
object An object expression that evaluates to an object in the Applies To list.
number A numeric expression specifying the number of characters selected. For SelLength and SelStart, the valid range of settings is 0 to text length — the total number of characters in the edit area of a ComboBox or TextBox control.
index A numeric expression specifying the starting point of the selected text, as described in Settings.
value A string expression containing the selected text.

Remarks

Use these properties for tasks such as setting the insertion point, establishing an insertion range, selecting substrings in a control, or clearing text. Used in conjunction with the Clipboard object, these properties are useful for copy, cut, and paste operations.

When working with these properties:

  • Setting SelLength less than 0 causes a run-time error.
  • Setting SelStart greater than the text length sets the property to the existing text length; changing SelStart changes the selection to an insertion point and sets SelLength to 0.
  • Setting SelText to a new value sets SelLength to 0 and replaces the selected text with the new string.

Example

This example enables the user to specify some text to search for and then searches for the text and selects it, if found. To try this example, paste the code into the Declarations section of a form that contains a wide TextBox control, and then press F5 and click the form.

Private Sub Form_Load ()
   Text1.Text = "Two of the peak human experiences"
   Text1.Text = Text1.Text & " are good food and classical music."
End Sub
Private Sub Form_Click ()
   Dim Search, Where   ' Declare variables.
   ' Get search string from user.
   Search = InputBox("Enter text to be found:")
   Where = InStr(Text1.Text, Search)   ' Find string in text.
   If Where Then   ' If found,
      Text1.SetFocus
      Text1.SelStart = Where - 1   ' set selection start and
      Text1.SelLength = Len(Search)   ' set selection length.
   Else
      MsgBox "String not found."   ' Notify user.
   End If
End Sub

See Also

SelText Property (Masked Edit Control) | Text Property (ActiveX Controls)

Applies To: Masked Edit Control

Show:
© 2014 Microsoft