This is a migrated thread and some comments may be shown as answers.

Clearing UI document selection programatically

2 Answers 97 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Juan
Top achievements
Rank 1
Juan asked on 20 Aug 2013, 01:00 AM
Hi,

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:

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

RadRichTextBox.Insert(newText);

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.

Cheers,

Juan

2 Answers, 1 is accepted

Sort by
0
Accepted
Petya
Telerik team
answered on 21 Aug 2013, 03:20 PM
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.
this.radRichTextBox.Document.Selection.SelectAnnotationRange(annotation);
 this.radRichTextBox.Document.Selection.Ranges.First.StartPosition.MoveToNext();
 this.radRichTextBox.Document.Selection.Ranges.First.EndPosition.MoveToPrevious();

I am looking forward to your reply.

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 >>
0
Juan
Top achievements
Rank 1
answered on 21 Aug 2013, 11:20 PM
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 :)

Cheers,

Juan

Tags
RichTextBox
Asked by
Juan
Top achievements
Rank 1
Answers by
Petya
Telerik team
Juan
Top achievements
Rank 1
Share this question
or