SelLength, SelStart, SelText Properties

See AlsoDM7XHN                 Example11XMRCNK>Low               Example21XMRCNL>Low

Apply To

Combo box1YZXFF6, text boxYPYZDG.

Description

         SelLengthdetermines the number of characters selected.

         SelStartdetermines the starting point of text selected; indicates the position of the insertion point if no text is selected.

         SelTextdetermines the string containing the currently selected text; consists of an empty string ("") if no characters are selected.

 

These properties are not available at design time.

Usage

[form.]{ combobox|textbox}.SelLength[ = length ]

[form.]{ combobox|textbox}.SelStart[ = index ]

[form.]{ combobox|textbox}.SelText[ = stringexpression ]

Setting

For SelLength and SelStart, the valid range of settings is 0 to text lengththe total number of characters in the edit area of a combo box or text box.

Remarks

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

When working with these properties:

         Setting SelLength less than 0 causes a run-time error52C78KR.

         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.

Data Type

SelLength, SelStartLong103F6YL

SelTextString7WSH0XQ


See Also

Help:

ActiveControl PropertyJCXG6P

ActiveForm PropertyGCUI9X

Text Property14TWSRU

 

Programmer's Guide:

Chapter 3, "Creating and Using Controls"


SelLength, SelStart, SelText Properties Example 1

This example lets the user specify some text to search for, and then searches 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 text box.  Then press F5 and click the form.

Sub Form_Load ()
  Text1.Text = "Two of the peak human experiences"
  Text1.Text = Text1.Text & " are good food and classical music."
End Sub

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.SelStart = Where - 1           ' Set selection start.
    Text1.SelLength = Len(Search)        ' Set selection length.
  Else
    MsgBox "String not found."           ' Notify user
  End If
End Sub


SelLength, SelStart, SelText Properties Example 2

This example shows how the Clipboard is used in cut, copy, paste, and delete operations.  To try this example, create a form with a text box and use the Menu Design window to create an Edit menu (for each of the commands, set Caption = Cut, Copy, Paste, and Delete; and set Name = EditCut, EditCopy, EditPaste, and EditDelete, respectively).

Sub EditCut_Click ()
  ' Copy selected text to Clipboard.
  ClipBoard.SetText Screen.ActiveControl.SelText
  ' Delete selected text.
  Screen.ActiveControl.SelText = ""
End Sub
Sub EditCopy_Click ()
  ' Copy selected text to Clipboard.
  ClipBoard.SetText Screen.ActiveControl.SelText
End Sub
Sub EditPaste_Click ()
  ' Place text from Clipboard into active control.
  Screen.ActiveControl.SelText = ClipBoard.GetText ()
End Sub

Sub EditDelete_Click ()
' Delete selected text.
Screen.ActiveControl.SelText = ""
End Sub