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

Get next/previous annotations on the same line (code sample)

1 Answer 93 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Robert
Top achievements
Rank 1
Robert asked on 09 May 2012, 02:51 PM
I have modified some code so that you can get the Next & Previous Annotations ONLY if they exist on the same line as the caret.


Get Previous Same Line Annotation:
public static T GetPreviousSameLineAnnotationRangeStart<T>(this RadDocument document) where T : AnnotationRangeStart
{
    //Document caret position
    DocumentPosition caretPosition = new DocumentPosition(document.CaretPosition);
    //Caret Line Info
    ParagraphLineInfo caretLineInfo = caretPosition.GetCurrentInlineBox().LineInfo;
 
    //Next Annotation LineInfo variable
    ParagraphLineInfo previousAnnotationLineInfo = null;
 
 
    if (!document.Selection.IsEmpty)
    {
        document.CaretPosition.MoveToPosition(document.Selection.Ranges.Last.StartPosition);
    }
 
 
    InlineLayoutBox inlineLayoutBox = document.CaretPosition.GetCurrentInlineBox();
    do
    {
        inlineLayoutBox = (InlineLayoutBox)DocumentStructureCollection.GetPreviousElementOfType(inlineLayoutBox, typeof(AnnotationMarkerLayoutBox));
    }
    while (inlineLayoutBox != null && !(inlineLayoutBox.AssociatedInline is AnnotationRangeStart));
 
 
    if (inlineLayoutBox == null)
    {
        return null;
    }
    else
    {
        //Get next annotation lineinfo if next annotation exists
        previousAnnotationLineInfo = inlineLayoutBox.LineInfo;
    }
 
 
    //Compare caret & next annotation lines
    if (previousAnnotationLineInfo != caretLineInfo)
    {
        return null;
    }
 
 
    return inlineLayoutBox.AssociatedInline as T;
}

Get Next Same Line Custom Annotation:
public static T GetNextSameLineAnnotationRangeStart<T>(this RadDocument document) where T : AnnotationRangeStart
{
    //Document caret position
    DocumentPosition caretPosition = new DocumentPosition(document.CaretPosition);
 
    //Caret Line Info
    ParagraphLineInfo caretLineInfo = caretPosition.GetCurrentInlineBox().LineInfo;
 
    //Next Annotation LineInfo variable
    ParagraphLineInfo nextAnnotationLineInfo = null;
 
     
 
    if (!document.Selection.IsEmpty)
    {
        document.CaretPosition.MoveToPosition(document.Selection.Ranges.Last.EndPosition);
    }
 
    InlineLayoutBox inlineLayoutBox = document.CaretPosition.GetCurrentInlineBox();
    while (inlineLayoutBox != null && !(inlineLayoutBox.AssociatedInline is AnnotationRangeStart))
    {
        inlineLayoutBox = (InlineLayoutBox)DocumentStructureCollection.GetNextElementOfType(inlineLayoutBox, typeof(AnnotationMarkerLayoutBox));
    }
 
 
    if (inlineLayoutBox == null)
    {
        return null;
    }
    else
    {
        //Get next annotation lineinfo if next annotation exists
        nextAnnotationLineInfo = inlineLayoutBox.LineInfo;
    }
 
 
    T annotationRangeStart = inlineLayoutBox.AssociatedInline as T;
    if (annotationRangeStart == null)
    {
        return null;
    }
 
    //Compare caret & next annotation lines
    if (nextAnnotationLineInfo != caretLineInfo)
    {
        return null;
    }
 
    return annotationRangeStart;
}


Here are some examples on how you can use these (note that the Get Next & Previous methods above are in a class file named RadDocumentExtensions. Change this to whatever your class name is):

Previous Same Line Annotation (change the last message box details to suit your custom annotation properties):
SemanticRangeStart sameLinePreviousAnnotation = RadDocumentExtensions.GetPreviousSameLineAnnotationRangeStart<SemanticRangeStart>(this.radRichTextBox.Document);
 
if (sameLinePreviousAnnotation == null)
{
    MessageBox.Show("No previous annotations");
}
else
{
    MessageBox.Show("Name: " + sameLinePreviousAnnotation.Product.Name.ToString() + "\n\nHas Child: " + sameLinePreviousAnnotation.Product.HasChild.ToString());
}


Next Same Line Annotation (change the last message box details to suit your custom annotation properties):

SemanticRangeStart sameLineNextAnnotation = RadDocumentExtensions.GetNextSameLineAnnotationRangeStart<SemanticRangeStart>(this.radRichTextBox.Document);
 
if (sameLineNextAnnotation == null)
{
    MessageBox.Show("No next annotations");
}
else
{
    MessageBox.Show("Name: " + sameLineNextAnnotation.Product.Name.ToString() + "\n\nHas Child: " + sameLineNextAnnotation.Product.HasChild.ToString());
}



Thought I should share this with everyone. There's no point all of us trying to do the same job twice!

Thanks,

Rob

1 Answer, 1 is accepted

Sort by
0
Accepted
Ivailo Karamanolev
Telerik team
answered on 11 May 2012, 11:24 AM
Hello,

Thanks for the code share.

Kind regards,
Ivailo Karamanolev
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
Ivailo Karamanolev
Telerik team
Share this question
or