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

Why does caret drop to line beneath? (Inserting annotation)

1 Answer 69 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Robert
Top achievements
Rank 1
Robert asked on 18 May 2012, 12:46 AM
Good evening,

Why does the following code force the caret to the line beneath? (obviously you must have multiple paragraphs in your document for this to happen).

In short I'm inserting a Annotation into an empty paragraph. The annotation when inserted does not encapsulate any content (spans etc), it will be filled with content at a later time.

RadRichTextBox radRichTextBox = ((RadRichTextBoxAutoComplete)currentPane).radRichTextBox;
 
//Set Custom Annotation
ItemGroupRangeEnd rangeEnd = new ItemGroupRangeEnd();
ItemGroupRangeStart rangeStart = (ItemGroupRangeStart)rangeEnd.CreatePairedStart();
rangeStart.Name = "GroupItem " + count++;
 
//Place annotation around newly inserted text
radRichTextBox.Document.InsertCustomAnnotationRange(radRichTextBox.Document.CaretPosition, radRichTextBox.Document.CaretPosition, rangeStart, rangeEnd);
 
//Referesh editor
radRichTextBox.UpdateEditorLayout();


It only does it if the following FieldRangeEnd property is set to "false".

public override bool SkipPositionBefore
{
    get
    {
        return false;
    }
}

If I set the return value to "true" then the caret does drop to the paragraph beneath but I don't get other behaviors that I want.

Thank you for your time,

Rob

1 Answer, 1 is accepted

Sort by
0
Accepted
Iva Toteva
Telerik team
answered on 23 May 2012, 04:47 PM
Hello Rob,

The problem is that the annotation range starts and ends do not take part in the "walk of the caret", i.e. if you have the following set-up: a[bbb]a, where "[" and "]" are the range start and end respectively, when you position the caret at the beginning and use the right arrow of the keyboard, it should move the caret in the following way:
1. First press - after the first "a";
2. Second press - after the first "b", etc.
The same thing occurs for the range end - if the caret is before the last "b", the right arrow will move it between "b" and "a" and if pressed once again - at the end of the whole word.

In order to achieve this behavior, the editor marks some positions as "invalid", so that it is not possible to navigate and add content at them. The value of the SkipPositionBefore is used to determine which positions are not valid. If the value of the property is "True", the editor considers that there is no document position before the annotation marker. That is why when you start typing at the position at which the range end is located, the text is not included in the range. Vice versa, if SkipPositionBefore="False", the editor considers that there is a position before the marker, but there is not after it. Therefore, if you start typing at the position of the range end marker, the text is included in the range.

That is why when SkipPositionBefore="False" and you insert the ranges, the caret finds the next "valid" position after the inserted chunk. If there is such position (i.e. the annotation is not inserted at the end of the document), the caret is moved after the annotation range end.

You basically have two choices:
1. You can position the caret before the annotation range end programmatically like this just after inserting the range:

radRichTextBox.Document.CaretPosition.MoveToPrevious();

In this way, the caret position will be in the same paragraph, but if a user starts typing, the text will be included in the annotation range.

2. You can set the SkipPositionBefore="True". In this way, the caret will stay at the end of the paragraph, but it will not be possible to add content in the range. This is how Bookmarks work for example.

I hope this answers your question.
Regards,
Iva Toteva
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Tags
RichTextBox
Asked by
Robert
Top achievements
Rank 1
Answers by
Iva Toteva
Telerik team
Share this question
or