Selection

Selects text in a RichTextBox, reads and replaces the selected text, and reports each change of the selection.

Dim Watch As New RtbSelectionWatcher
RichTextBox1.Text = "Hello world"
Set Watch.Box = RichTextBox1
RichTextBox1.SelStart = 6
RichTextBox1.SelLength = 5
RichTextBox1.SelText = "there"          ' replaces "world"
Debug.Print RichTextBox1.Text; RichTextBox1.SelStart
' Output:
' SelChange 0  6  6
' SelChange 5  6  11
' SelChange 0  11  11
' Change
' Hello there 11

RtbSelectionWatcher in this sample is a class of the program’s own that holds the control WithEvents and prints its SelChange and Change events.

The selection and the insertion point

SelStart and SelLength are the place and the length of the selection, counted in characters from 0, and SelText is the selected text, as they are in a TextBoxW. A line break counts as one character, a carriage return. With a SelLength of 0, SelStart is the insertion point, and the properties of the character and paragraph formats apply to the text typed there next.

SelRTF is the selection with its formatting, as a document in the Rich Text Format. An assignment to it replaces the selection with the text and the formatting of the document.

RichTextBox1.Text = "a small word"
RichTextBox1.SelStart = 2
RichTextBox1.SelLength = 5
RichTextBox1.SelRTF = "{\rtf1 \i big\i0}"   ' "big" in italics, in place of "small"
Debug.Print RichTextBox1.Text
RichTextBox1.SelStart = 2
RichTextBox1.SelLength = 3
Debug.Print RichTextBox1.SelItalic
' Output:
' a big word
' True

The kind of selection

GetSelType says what the selection holds, as a sum of the members of RtfSelTypeConstants: RtfSelTypeText for text, RtfSelTypeObject for an OLE object, and RtfSelTypeMultiChar and RtfSelTypeMultiObject when there is more than one of each. A selection of several characters is therefore 5, and the insertion point alone is RtfSelTypeEmpty. SelChange gives the same sum.

GetTextRange returns the text of any range without selecting it.

RichTextBox1.Text = "Hello world"
Debug.Print RichTextBox1.GetTextRange(0, 5); "|"; RichTextBox1.GetTextRange(6, 100)
RichTextBox1.SelStart = 0
RichTextBox1.SelLength = 5
Debug.Print RichTextBox1.GetSelType = RtfSelTypeText + RtfSelTypeMultiChar
' Output:
' Hello|world
' True

Properties

HideSelection

Whether the selection stops being shown when the box loses the focus. Boolean. Default: True.

SelLength

The number of characters selected. Long. An assignment selects that many characters from SelStart on, or as many as there are, and scrolls the selection into view.

A negative value raises run-time error 380.

SelRTF

The selection with its formatting, as a document in the Rich Text Format. String. See The selection and the insertion point.

SelStart

The place of the first selected character, counted from 0, or of the insertion point when nothing is selected. Long. An assignment puts the insertion point there, selects nothing, and scrolls the place into view.

A value greater than the length of the text puts the insertion point at the end. A negative value raises run-time error 380.

SelText

The selected text, without its formatting, with vbCr for a line break. String. An assignment replaces the selection with the new text, or inserts the text at the insertion point, leaves the insertion point after it and raises Change. The user can take the replacement back with Undo.

Methods

GetSelType

Returns what the selection holds, as a sum of the members of RtfSelTypeConstants.

Syntax: object.GetSelType As Integer

GetTextRange

Returns the text of a range of characters, without selecting it.

Syntax: object.GetTextRange( Min, Max ) As String

Min
required A Long: the place of the first character, counted from 0.
Max
required A Long: the place after the last character. A place after the end of the text gives the text up to the end.

A Min greater than Max raises run-time error 380. A Min equal to Max returns an empty string.

Events

SelChange

Raised when the selection or the insertion point has changed, whether the user or the program changed it.

Syntax: object_SelChange( ByVal SelType As Integer, ByVal SelStart As Long, ByVal SelEnd As Long )

SelType
What the selection holds, a sum of the members of RtfSelTypeConstants.
SelStart
The place of the first selected character, or of the insertion point.
SelEnd
The place after the last selected character. It equals SelStart when nothing is selected.

RtfSelTypeConstants

The values that GetSelType and SelChange add up.

Member Value Description
RtfSelTypeEmpty 0 Nothing is selected.
RtfSelTypeText 1 The selection holds text.
RtfSelTypeObject 2 The selection holds an OLE object.
RtfSelTypeMultiChar 4 The selection holds more than one character.
RtfSelTypeMultiObject 8 The selection holds more than one OLE object.

See Also