Skip to main content

DevExpress v24.2 Update — Your Feedback Matters

Our What's New in v24.2 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.

Take the survey Not interested

Selection Class

Contains information about document selection.

Namespace: DevExpress.Blazor.RichEdit

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

NuGet Package: DevExpress.Blazor.RichEdit

#Declaration

C#
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.

Razor
<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.

Razor
<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.

Razor
<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.

Razor
<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.

Razor
<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