Clearing UI document selection programatically

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

    Posted 19 Aug 2013 Link to this post


    I have an interesting issue with the selected text in my document within a RadRichTextBox.

    I am handling the MouseLeftButtonUp event, and in it I want to do the following:

    1. Select a specific section of the document
    2. Substitute that text with other content from a pop-up window (user enters new text)

    To do that, I use the following code to perform point 1:


    where 'document' is the RadRichTextBox.Document object, and 'annotation' is an AnnotationRangeStart object (never mind all this though, the issue is that I want to clear any previous selection in the document and create a new selection).

    Then I do point 2 after my pop-up has been closed, simply by:


    All this works fine if there is no previous selection in the document, and the user simply clicks anywhere in the document: the MouseLeftButtonUp fires and I can do everything I intend to do as mentioned. HOWEVER, if instead of just clicking the user clicks+holds+selects some content in the document, when the MouseLeftButtonUp fires I get the following behavior:

    - Even though when debugging it SEEMS like the document.Selection is actually clearing and changing when the code for point 1 executes, actually the UI still keeps the user selection (?). I can't find how/where those 2 different selections (user/UI created and the programmatically created) are stored, so as to really remove the user-created selection. So the problem is that, when I execute the code for point 2, is not really my programmatically created selection that gets substituted, but instead it's the user-created selection (not the intended behavior).

    - I have tried to set to 'true' the Handled property of the MouseButtonEventArgs parameter on the MouseLeftButtonUp handler, so as to "invalidate" the creation of the UI selection... but instead of that, the behavior I get is that the MouseLeftButtonUp event is actually not finalized its handling so when I return to the RadRichTextBox the caret is in "selection mode" (hope I have explained this to make myselfunderstood :P). I've also tried to handle the MouseLeftButtonDown event instead, all to "avoid" the creation of the UI selection... but that doesn't work either (the behavior is even more strange).

    Any help with this is much appreciated.



  2. Answer
    Petya avatar
    973 posts

    Posted 21 Aug 2013 Link to this post

    Hello Juan,

    I tried replicating the issue you are observing with the code you provided but was unable to. Further, we are unaware of cases where the Selection.Clear() method does not work properly.

    However, when it comes to annotations, selecting them and replacing their content requires special attention. This is so as the SkipPositionBefore property of annotation markers affects the behavior. For example, the end marker of a bookmark has this property set to true so selecting its content and inserting in its place will result in the content being inserted after the annotation end.

    That said would you mind sharing what type of annotations you are selecting exactly? Any additional details you feel are relevant will also be appreciated.

    On a side note, I suggest you modify the code that selects the content of the annotation to use the SelectAnnotationRange() method as it is not recommended to modify the caret position that often and the implemented logic might cause issues.

    I am looking forward to your reply.

    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 R3 2016 release webinar banner
  4. Juan
    Juan avatar
    5 posts
    Member since:
    Aug 2013

    Posted 21 Aug 2013 Link to this post

    Hi, and thanks for the answer.

    You are right I should have mentioned the type of annotations we are working with, in case it was relevant: they are PermissionRange annotation types.

    Anyhow, I must say the proposed alternative to select the annotation content and then replace it SOLVED the issue I was experiencing! If you want to test it, I suggest to create a simple document with a PermissionRange and content (text) in it, then select from the RadRichTextBox part of that content and handle the MouseLeftButtonUp: as mentioned, what I experienced was that selecting the annotation content and replacing it with the code I posted (using the CaretPosition) was not working, since only the text previously selected by the user was replaced and not the whole content of the annotation I had programatically selected. It could have been a case of what you mention as "the implemented logic might cause issues" when using the CaretPosition :)



Back to Top