Edit this page

Selection

The RadRichTextBox supports not only selection via the UI, but also programmatic selection. This topic covers the following:

UI Selection

The user is able to select the content of the RadRichTextBox in the same way as in MS Word. This is done by clicking on the desired position and dragging to the desired end of the selection. A multiple ranges selection is also allowed. This one is done by holding the Ctrl key while selecting the different ranges.

Programmatic Selection

The developer is allowed to work with the selection programmatically. This can be used when implementing Search functionality functionality in your RadRichTextBox and you want to select the found string.

The programmatic selection gets implemented via the DocumentSelection class. The instance of the class gets associated with the RadDocument of the RadRichTextBox and allows you to specify selection starts and ends, selection ranges and other. You can manage the selection by either using the Selection property of the RadDocument or by creating an instance of the DocumentSelection class.

Here is an example of how to select the current word.

To learn more about the DocumentPosition read the Positioning topic.

C#

DocumentPosition startPosition = new DocumentPosition(this.radRichTextBox.Document.CaretPosition);
DocumentPosition endPosition = new DocumentPosition(startPosition);
startPosition.MoveToCurrentWordStart();
endPosition.MoveToCurrentWordEnd();
this.radRichTextBox.Document.Selection.SetSelectionStart(startPosition);
this.radRichTextBox.Document.Selection.AddSelectionEnd(endPosition);

VB.NET

Dim startPosition As New DocumentPosition(Me.radRichTextBox.Document.CaretPosition)
Dim endPosition As New DocumentPosition(startPosition)
startPosition.MoveToCurrentWordStart()
endPosition.MoveToCurrentWordEnd()
Me.radRichTextBox.Document.Selection.SetSelectionStart(startPosition)
Me.radRichTextBox.Document.Selection.AddSelectionEnd(endPosition)

Multi-Range Selection

You can implement multi-range Selection by either calling multiple times the AddSelectionStart() and AddSelectionEnd() methods or by working with the Ranges collection.

Here is an example of selecting each "RadRichTextBox" word in the text. This example uses the first approach.

C#

DocumentPosition position = new DocumentPosition(this.radRichTextBox.Document);
do
{
    //GetCurrentSpan().Text returns the word at the position
    string word = position.GetCurrentSpanBox().Text;
    if (word.Contains("RadRichTextBox"))
    {
        DocumentPosition wordEndPosition = new DocumentPosition(position);
        wordEndPosition.MoveToCurrentWordEnd();
        this.radRichTextBox.Document.Selection.AddSelectionStart(position);
        this.radRichTextBox.Document.Selection.AddSelectionEnd(wordEndPosition);

    }
} 
while (position.MoveToNextWordStart());

VB.NET

Dim position As New DocumentPosition(Me.radRichTextBox.Document)
Do
 \'GetCurrentSpan().Text returns the word at the position
 Dim word As String = position.GetCurrentSpanBox().Text
 If word.Contains("RadRichTextBox") Then
  Dim wordEndPosition As New DocumentPosition(position)
  wordEndPosition.MoveToCurrentWordEnd()
  Me.radRichTextBox.Document.Selection.AddSelectionStart(position)

  Me.radRichTextBox.Document.Selection.AddSelectionEnd(wordEndPosition)
 End If
Loop While position.MoveToNextWordStart()

When working with ranges, you may need to check what elements are included in a selection range. This could be achieved with the RangeType property of SelectionRange. This property is of type SelectionRangeType and could have one of the following values:

  • Composite: Represents selection range with mixed elements.

  • Table: SelectionRange, which contains a Table.

  • TableRow: The range consists of a TableRow.

  • TableCell: TableCell selection range.

C#

SelectionRange selectionRange = this.radRichTextBox.Document.Selection.Ranges.First();
if (selectionRange.RangeType == SelectionRangeType.Table)
{
    SetTableProperties();
}

VB.NET

Dim selectionRange As SelectionRange = Me.radRichTextBox.Document.Selection.Ranges.First()
If selectionRange.RangeType = SelectionRangeType.Table Then
    SetTableProperties()
End If

See Also