Skip to main content

Selection Class

Contains information about document selection.

Namespace: DevExpress.Blazor.RichEdit

Assembly: DevExpress.Blazor.RichEdit.v24.1.dll

NuGet Package: DevExpress.Blazor.RichEdit

Declaration

public class Selection

The following members return Selection objects:

Remarks

The Selection object contains the following information:

To access the editor’s selection in code, bind the Selection property with @bind-Selection attribute.

<DxRichEdit @bind-Selection="@selection" @ref="@richEdit" />

@code {
    DxRichEdit richEdit;
    Selection selection;
    @* ... *@
    /* Surround the code that contains an asynchronous operation with a try-catch block to handle
    the OperationCanceledException. This exception is thrown when an asynchronous operation is canceled. */
        try {
        @* ... *@
            // Applies bold formatting to characters in the selected intervals.
            IReadOnlyList<Interval> intervals = selection.Intervals;
            foreach (Interval interval in intervals) {
                TextSpan boldTextSpan = await richEdit.DocumentAPI.GetTextSpanAsync(interval);
                await boldTextSpan.ChangePropertiesAsync(properties => {
                    properties.FontBold = true;
                });
            }
            @* ... *@
        }
        catch (OperationCanceledException e) {
            Console.WriteLine($"{nameof(OperationCanceledException)} thrown with message: {e.Message}");
        }
}

Set Selection

Call the Selection object’s constructor to create a new selection in Rich Text Editor.

Set the Caret Position

Call the Selection(SubDocument, Int32) constructor to set the caret position.

<DxRichEdit @bind-Selection="@selection" @ref="@richEdit" />

@code {
    DxRichEdit richEdit;
    Selection selection;
    @* ... *@
    /* Surround the code that contains an asynchronous operation with a try-catch block to handle
    the OperationCanceledException. This exception is thrown when an asynchronous operation is canceled. */
        try {
        @* ... *@
            selection = new Selection(richEdit.DocumentAPI, 8);
            @* ... *@
        }
        catch (OperationCanceledException e) {
            Console.WriteLine($"{nameof(OperationCanceledException)} thrown with message: {e.Message}");
        }
}

Rich Edit - Set the caret position

Select an Interval

Call the Selection(SubDocument, Int32, Int32) constructor to select an interval.

<DxRichEdit @bind-Selection="@selection" @ref="@richEdit" />

@code {
    DxRichEdit richEdit;
    Selection selection;
    @* ... *@
    /* Surround the code that contains an asynchronous operation with a try-catch block to handle
    the OperationCanceledException. This exception is thrown when an asynchronous operation is canceled. */
        try {
        @* ... *@
            selection = new Selection(richEdit.DocumentAPI, 4, 23);
            @* ... *@
        }
        catch (OperationCanceledException e) {
            Console.WriteLine($"{nameof(OperationCanceledException)} thrown with message: {e.Message}");
        }
}

Rich Edit - Select an interval

Select Several Intervals

Call the Selection(SubDocument, Interval[]) constructor to select several intervals.

<DxRichEdit @bind-Selection="@selection" @ref="@richEdit" />

@code {
    DxRichEdit richEdit;
    Selection selection;
    @* ... *@
    /* Surround the code that contains an asynchronous operation with a try-catch block to handle
    the OperationCanceledException. This exception is thrown when an asynchronous operation is canceled. */
        try {
        @* ... *@
            Interval[] myIntervals = { new Interval(4, 6), new Interval(97, 6) };
            selection = new Selection(richEdit.DocumentAPI, myIntervals);
            @* ... *@
        }
        catch (OperationCanceledException e) {
            Console.WriteLine($"{nameof(OperationCanceledException)} thrown with message: {e.Message}");
        }
}

Rich Edit - Select several intervals

SelectionChanged Event

Handle the SelectionChanged event to react to selection changes.

<DxRichEdit Selection="@richSelection" SelectionChanged="OnSelectionChanged" />

@code {
    Selection richSelection { get; set; }

    async Task OnSelectionChanged(Selection newSelection) {
    /* Surround the code that contains an asynchronous operation with a try-catch block to handle
    the OperationCanceledException. This exception is thrown when an asynchronous operation is canceled. */
        try {
            richSelection = newSelection;
            // Your code
        }
        catch (OperationCanceledException e) {
            Console.WriteLine($"{nameof(OperationCanceledException)} thrown with message: {e.Message}");
        }
    }
}

Inheritance

Object
Selection
See Also