RichTextBox.Selection Property

Definition

Gets a TextSelection object containing the current selection in the RichTextBox.

C#
public System.Windows.Documents.TextSelection Selection { get; }

Property Value

A TextSelection object containing the current selection in the RichTextBox.

The default returned TextSelection has an IsEmpty property value of true. An empty TextSelection renders as a caret in the text area with no selection.

Examples

The following example illustrates the use of this property.

C#
// Create a simple FlowDocument to serve as the content input for the construtor.
FlowDocument flowDoc = new FlowDocument(new Paragraph(new Run("Simple FlowDocument")));
// After this constructor is called, the new RichTextBox rtb will contain flowDoc.
RichTextBox rtb = new RichTextBox(flowDoc);
// This call will select the entire contents of the RichTextBox.
rtb.SelectAll();
// This call returns the current selection (which happens to be the entire contents
// of the RichTextBox) as a TextSelection object.
TextSelection currentSelection = rtb.Selection;

Remarks

Use the Select method to programmatically select content.

Applies to

Produk Versi
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10