Selection
RadRichTextBox supports not only selection via the UI, but also programmatic selection. This topic shows how you can use both approaches.
UI Selection
The user is able to select the content inside 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.
You can modify the appearance of the selection in the control through the SelectionFill and SelectionStroke properties of RadRichTextBox.
Customize the appearance of the selection
<telerik:RadRichTextBox Name="radRichTextBox" SelectionStroke="DarkRed">
<telerik:RadRichTextBox.SelectionFill>
<SolidColorBrush Color="Red" Opacity="0.5" />
</telerik:RadRichTextBox.SelectionFill>
</telerik:RadRichTextBox>
Customize the appearance of the selection
this.radRichTextBox.SelectionFill = new System.Windows.Media.SolidColorBrush(Colors.Red);
this.radRichTextBox.SelectionFill.Opacity = 0.2;
this.radRichTextBox.SelectionStroke = System.Windows.Media.Brushes.DarkRed;Custom selection colors

Advanced Selection Mode
By default when the user select text the words are selected letter by letter. We have introduced an advanced selection mode that allow you to select whole word automatically. The automatic words selection begins after the selection leaves the first word. This mode can be enabled by the IsAdvancedSelectionEnabled property.
Programmatic Selection
The developer is allowed to manipulate the selection programmatically. This can be used in a lot of scenarios involving editing of the document, or for example, when implementing Search functionality in your RadRichTextBox and you want to select the found string.
The programmatic selection is represented via the DocumentSelection class. The instance of the class gets associated with the RadDocument of RadRichTextBox and allows you to specify selection start and end positions, selection ranges and other. You can manage the selection by either using the Selection property of RadDocument or by creating an instance of the DocumentSelection class.
To learn more about the
DocumentPositionread the Positioning topic.
Members of DocumentSelection
Properties:
Ranges—Holds a collection of all SelectionRange instances in the selection.IsEmpty—Holds a value determining whether the selection is empty or not.
Methods for manipulating the selection:
SelectAll—Selects all the content of the document.SetSelectionStart—Empties the selection and begins a new SelectionRange starting at the specified position. This method starts the creation of a range, which should be finished with theAddSelectionEndmethod.AddSelectionStart—Begins a new SelectionRange starting at the specified position. This method starts the creation of a range, which should be finished with theAddSelectionEndmethod.AddSelectionEnd—Finishes the started SelectionRange by setting its end.SelectAnnotationRange—Selects annotation range including the AnnotationRangeStart and AnnotationRangeEnd elements.SelectTableRow—Creates a new selection containing the specific table row.SelectTableColumn—Creates a new selection containing all the cells in a table column. There are two overloads that allow you to pass a cell from the column or the table and the grid column index, which should be selected.AddDocumentElementToSelection—Adds a specified document element to the existing selection.AddTableCellToSelection—Adds a table cell to the existing selection.AddParagraphToSelection—Adds a paragraph to the existing selection.
Obtaining information and elements from the selection:
CopySelectedDocumentElements—Copies all selected document elements to a DocumentFragment instance.GetSelectedText—Gets the text from the selection. Offers an overload allowing you to specify whether the result should include the bullet or numbering characters in case a paragraph is in a list.GetSelectedParagraphs—Gets the paragraphs included in the selection.GetSelectedBoxes<T>—Enumerates through all inline layout boxes of typeTincluded in the selected ranges.GetFullSelectionGeometry—Gets a System.Windows.Media.Geometry instance describing the form of the selection.GetVisibleSelectionGeometry—Gets a System.Windows.Media.Geometry instance describing the form of the selection visible in the rectangle passed as a parameter.CreateDocumentFromSelection—Creates a new RadDocument instance containing the selected elements.ContainsAnnotationMarkersOfType<T>—Determines whether the selection contains annotation markers of typeT.GetAnnotationMarkersOfType<T>—Gets all annotation markers of typeTin the selection.GetSelectedSingleInline—Gets the selected Inline if it is the only inline selected, otherwise returns null. This method is suitable to check if only anImageInlineis selected, for example.ToString—Gets the text from the selected document elements.
Clearing the selection method:
Clear—Empties the selection.
Events:
SelectionChanging—Occurs before the selection is changed.SelectionChanged—Occurs after the selection is changed. Such an event is exposed by the RadRichTextBox class as well.
Select current word
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);The next snippet demonstrates how you can check the content that is selected and select and delete the current paragraph if the text in the selection contains the word "Test".
Select current paragraph and delete it
string selectedText = this.radRichTextBox.Document.Selection.GetSelectedText();
if (selectedText.Contains("Test"))
{
this.radRichTextBox.Document.Selection.Clear();
Paragraph currentParagraph = this.radRichTextBox.Document.CaretPosition.GetCurrentParagraph();
this.radRichTextBox.Document.Selection.AddDocumentElementToSelection(currentParagraph);
this.radRichTextBox.Delete(true);
}What is Selection Range?
The selection in RadRichTextBox consists of selection ranges. These ranges are represented by the SelectionRange class which exposes the following members:
-
StartPosition