SelLength, SelStart, SelText Properties

Purpose

SelLength returns or sets the number of characters selected; SelLine returns the current line in a Richedit object; SelStart returns or sets the starting point of text selected or indicates the position of the insertion point if no text is selected; SelText returns or sets the string containing the currently selected text.

Syntax

object.SelLength [= number]
Richedit.SelLine
object.SelStart [= index]
object.SelText [= string]

object: TextBox,RichEdit

Description

SelLength = number specifies 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 TextBox or Richedit control.

The SelLine property is specific to Richedit objects and returns the current line number.

SelStart = index specifies the starting point of the selected text.

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.

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.

SelText returns or sets plain text. Setting new selected text sets SelLength to 0 and replaces the selected text with the new string. Richedit controls have an additional property SelRTF - see here for more information.

Example

OpenW 1

Ocx RichEdit TBox1 = "", 10, 10, 300, 300

TBox1.BorderStyle = 1

TBox1.Text = "This is text"#13#10"This is more text"#13#10"This is even more text"

TBox1.FontBold = 0

TBox1.BackColor = RGB(224, 224, 224)

TBox1.ForeColor = RGB(255, 0, 0)

TBox1.SelStart = 23

TBox1.SelLength = 4

TBox1.SetFocus

DoEvents                                        // Draws the TBox1 Richedit object

Output = Win_1                                  // Sets output to the main window

Text 320, 10, "Selected Line: " & TBox1.SelLine // Prints the current line number

Debug TBox1.SelText : Debug.Show                // Sends the selected text to the debug screen

TBox1.SelText = "...."                          // Changes the selected text to "...."

Do : Sleep : Until Win_1 Is Nothing

See Also

TextBox, RichEdit, Ocx

{Created by Sjouke Hamstra; Last updated: 16/07/2020 by James Gaite}