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:
Get Next Same Line Custom Annotation:
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):
Next Same Line Annotation (change the last message box details to suit your custom annotation properties):
Thought I should share this with everyone. There's no point all of us trying to do the same job twice!
Thanks,
Rob
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