"Read" content inside annotation

4 posts, 0 answers
  1. Juan
    Juan avatar
    5 posts
    Member since:
    Aug 2013

    Posted 19 Aug 2013 Link to this post

    Hi.

    I have a quite simple query. In our project we have the occasional need to "read" the content inside an annotation, sometimes just to know what it is and others to manipulate it (change it in some way). Well, the way we're currently doing it is like this:

                document.Selection.Clear();
                document.CaretPosition.MoveToInline(annotation);
                document.CaretPosition.MoveToNext();
                document.Selection.AddSelectionStart(document.CaretPosition);
                document.CaretPosition.MoveToInline(annotation.End);
                document.Selection.AddSelectionEnd(document.CaretPosition);

    ... where 'document' is the RadRichTextBox.Document and 'annotation' is an AnnotationRangeStart object. So, in essence, we are navigating the caret to select the content inside the annotation, and then we can directly read the document.Selection and obtain that content inside the annotation (and, when need it, completely replace the selection, for instance).

    So, my question is: isn't there a more direct way to do this? It just seems to me like a lot of navigation, specially after we have previously searched for the annotation object... although it may just be the best way to do it, in which case this could serve somebody else :)

    Cheers,

    Juan
  2. Petya
    Admin
    Petya avatar
    975 posts

    Posted 21 Aug 2013 Link to this post

    Hello Juan,

    DocumentSelection exposes a SelectAnnotationRange() method which selects an annotation based on its start. Note that the method will include the actual range start and end in the selection, so if you want to only select the content you may need to move the start and end of the selection range:
    this.radRichTextBox.Document.Selection.SelectAnnotationRange(annotation);
     this.radRichTextBox.Document.Selection.Ranges.First.StartPosition.MoveToNext();
     this.radRichTextBox.Document.Selection.Ranges.First.EndPosition.MoveToPrevious();

    Additionally, please note that when working with document positions in order to select it is not usually recommended to directly use the caret position, as that might cause unexpected behavior. Instead, you should create a new document position and move it to the desired location.

    I hope this helps!

    Regards,
    Petya
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for SILVERLIGHT.
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
  3. DevCraft banner
  4. Juan
    Juan avatar
    5 posts
    Member since:
    Aug 2013

    Posted 21 Aug 2013 Link to this post

    Hi Petya.

    Thanks very much for your answer. I have tested what you propose and it does work like a charm (as could be expected :)

    Just one quick follow-up question, though, regarding your last paragraph: I have tested and confirmed that your proposed method does work better than what we were using before in certain instances that were causing us trouble (specifically: when there was a previous selection from the user, changing the selection programatically the way I was describing was not working, whereas like you propose actually does override the user selection and change it with the new one). However, since we are directly modifying the RadRichTextBox.Document.Selection property, aren't we indeed ALSO changing the caret position? I have tested this and it seems like it actually is (which is fine in our scenario, it's what we want). I understand it would NOT change the caret position if all we do is create a new instance of a DocumentPosition and get some content from there, but since we're actually changing the Document.Selection, caret is re-positioned too, am I correct?

    Thanks!

    Juan
  5. Petya
    Admin
    Petya avatar
    975 posts

    Posted 26 Aug 2013 Link to this post

    Hello Juan,

    Actually, the proposed method for selecting an annotation should not change the caret position. I presume an additional action such as insertion is changing the position of the caret in your project. You can try subscribing to the PositionChanged event of the CaretPosition if you want to debug this.

    The reason the caret position is not changed is that explicitly moving the caret position clears the selection by default. However, the DocumentSelection class provides you with the ability to select multiple Ranges at a time, so when you invoke methods like SelectAnnotationRange() you would not expect for the previous selection ranges to be cleared.

    As an example of this, if you run the following code over a document containing multiple permission ranges all of them will be selected while the caret will not change its position:
    foreach (var annotation in this.radRichTextBox.Document.EnumerateChildrenOfType<PermissionRangeStart>())
    {
        this.radRichTextBox.Document.Selection.SelectAnnotationRange(annotation);
    }
    Now, if you change the caret as follows:
    this.radRichTextBox.Document.CaretPosition.MoveToLastPositionInDocument();
    all previously added selection ranges will be removed.

    To further explain the point I made in my previous post, RadRichTextBox's API greatly depends on the caret position, so we usually do not recommend modifying its position manually by moving it unless moving the caret is exactly what you want to do. For all other purposes you can create a separate DocumentPosition.

    Furthermore, in case you need to move the caret while preserving the selection, you can always create a DocumentSelection object in code and store the current document selection there while operating with the caret.

    I hope this makes things clear! Let us know in case you have any additional comments.

    Regards,
    Petya
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for SILVERLIGHT.
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
Back to Top