Back to Devexpress

Document.Selection Property

officefileapi-devexpress-dot-xtrarichedit-dot-api-dot-native-dot-document-34103782.md

latest8.4 KB
Original Source

Document.Selection Property

Gets or sets the selected range in the document.

Namespace : DevExpress.XtraRichEdit.API.Native

Assembly : DevExpress.RichEdit.v25.2.Core.dll

NuGet Package : DevExpress.RichEdit.Core

Declaration

csharp
DocumentRange Selection { get; set; }
vb
Property Selection As DocumentRange

Property Value

TypeDescription
DocumentRange

A range that the selected text occupies.

|

Remarks

Use the Document.Copy, Document.Cut and Document.Paste methods without parameters to work with selection in the document.

The DocumentRange.Start and DocumentRange.End properties allows you to obtain the selection’s start and end positions.

Use the SubDocument.GetText method to obtain the plain text contained in the selection.

When you select individual table cells, the Selection property returns the last selected range. Use the Document.Selections property to obtain a collection of selections in the document. You can use the SelectionCollection to add new selection range or unselect previously selected range.

Example

View Example

csharp
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;

static void SelectTextInRange(Document document) {

    document.LoadDocument("Grimm.docx", DocumentFormat.Docx);
    DocumentPosition myStart = document.CreatePosition(69);
    DocumentRange myRange = document.CreateRange(myStart, 216);
    document.Selection = myRange;
}
vb
Imports DevExpress.XtraRichEdit
Imports DevExpress.XtraRichEdit.API.Native

Shared Sub SelectTextInRange(ByVal document As Document)

    document.LoadDocument("Grimm.docx", DocumentFormat.Docx)
    Dim myStart As DocumentPosition = document.CreatePosition(69)
    Dim myRange As DocumentRange = document.CreateRange(myStart, 216)
    document.Selection = myRange
End Sub

The following code snippets (auto-collected from DevExpress Examples) contain references to the Selection property.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

winforms-richedit-save-text-from-a-range-in-different-formats/CS/GetTextMethodsExample/Form1.cs#L31

csharp
{
    DevExpress.XtraRichEdit.API.Native.DocumentRange selection = richEditControl.Document.Selection;
    DevExpress.XtraRichEdit.API.Native.SubDocument doc = selection.BeginUpdateDocument();

winforms-richedit-document-api/CS/RichEditAPISample/CodeExamples/Range.cs#L15

csharp
DocumentRange myRange = document.CreateRange(myStart, 216);
document.Selection = myRange;
#endregion #SelectTextInRange

winforms-richeditcontrol-common-api/CS/RichEditAPISample/CodeExamples/RichEditControlActions.cs#L49

csharp
DocumentRange[] buttonWordRanges = richEdit.Document.FindAll("button", SearchOptions.None);
richEdit.Document.Selection = buttonWordRanges[0];
richEdit.ShowReplaceForm();

how-to-customize-copy-and-paste-commands/CS/RichEditCustomCopyPaste/CustomCommands.cs#L25

csharp
try {
    string html = Control.Document.GetHtmlText(Control.Document.Selection, new CustomUriProvider(), richEditControl.Options.Export.Html);
    htmlForClipboard = CF_HTMLHelper.GetHtmlClipboardFormat(html);

winforms-rich-edit-implement-ms-office-word-format-painter/CS/DXApplication9/Form1.cs#L84

csharp
{
    DocumentRange selection = richEditControl.Document.Selection;
    SubDocument subDocument = selection.BeginUpdateDocument();

winforms-richedit-save-text-from-a-range-in-different-formats/VB/GetTextMethodsExample/Form1.vb#L27

vb
If Me.richEditControl.Document.Selection.Length > 0 Then
    Dim selection As DevExpress.XtraRichEdit.API.Native.DocumentRange = richEditControl.Document.Selection
    Dim doc As DevExpress.XtraRichEdit.API.Native.SubDocument = selection.BeginUpdateDocument()

winforms-richedit-document-api/VB/RichEditAPISample/CodeExamples/Range.vb#L14

vb
Dim myRange As DevExpress.XtraRichEdit.API.Native.DocumentRange = document.CreateRange(myStart, 216)
            document.Selection = myRange
#End Region ' #SelectTextInRange

winforms-richeditcontrol-common-api/VB/RichEditAPISample/CodeExamples/RichEditControlActions.vb#L54

vb
Dim buttonWordRanges() As DocumentRange = richEdit.Document.FindAll("button", SearchOptions.None)
richEdit.Document.Selection = buttonWordRanges(0)
richEdit.ShowReplaceForm()

how-to-customize-copy-and-paste-commands/VB/CustomCommands.vb#L27

vb
Try
    Dim html As String = Control.Document.GetHtmlText(Control.Document.Selection, New CustomUriProvider(), richEditControl.Options.Export.Html)
    htmlForClipboard = CF_HTMLHelper.GetHtmlClipboardFormat(html)

winforms-rich-edit-implement-ms-office-word-format-painter/VB/DXApplication9/Form1.vb#L76

vb
Private Sub SaveSelectedRange()
    Dim selection As DocumentRange = richEditControl.Document.Selection
    Dim subDocument As SubDocument = selection.BeginUpdateDocument()

See Also

GetText

Document Interface

Document Members

DevExpress.XtraRichEdit.API.Native Namespace