Modify selection contents

2 posts, 0 answers
  1. Mihajlo
    Mihajlo avatar
    47 posts
    Member since:
    Aug 2018

    Posted 27 Nov 2018 Link to this post

         Let's say I want to give my user the ability to select some text and convert it to some other text, for instance to mangle it with ROT13 cipher. And selection could be made of several ranges, including in table cells. How would I go about this? My first idea was something like the code below, but some kind of update is required, because I only see the change if I scroll the text out view and back in, and even then the changed text does not reflow, but it continues after the right margin. Also, ability to undo the changes would be nice.

    foreach (var inlineLayoutBox in radRichTextEditor1.Document.Selection.GetSelectedBoxes())
        if (inlineLayoutBox.Text.Contains('e'))
            inlineLayoutBox.Text = inlineLayoutBox.Text.Replace("e", "3333333");


  2. Tanya
    Tanya avatar
    922 posts

    Posted 30 Nov 2018 Link to this post

    Hello Mihajlo,

    With the current approach, there is a direct change in the model of the document. As a result, the rendering is not notified of the change, thus it is not updated. What you can do to update the document is to invoke the InvalidateMeasureUpToTheRoot() method on each of the layout boxes that are changed and the UpdateEditorLayout() of RadRichTextBox to force the measuring processes and update the visualization.

    My concern here is that using this approach might be a bit risky - the layout boxes are responsible for the rendering and a single span can be represented by several layout boxes. This on its turn, might lead to incorrect behavior in the application. For example, a span can be separated in different layout boxes if a part of its letters has a different styling.

    What I can suggest as an alternative implementation is to use the DocumentTextSearch class, which allows you to apply a particular range to search inside through the overloads of the Find() method. You can iterate the ranges of the selection, obtain their positions and perform the search operation on them. Something to note here is that would be a better solution to iterate the selection ranges and the matches from the search backward - from the last item to the first one to avoid additional updates.

    Hope this information is helpful.

    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Back to Top