5 Answers, 1 is accepted
Yes, the approach is pretty straight-forward.
MergeFields are a type of Fields and are represented in the document through the inline document elements FieldRangeStart and FieldRangeEnd.
Therefore, you can search for MergeFields using the EnumerateChidlrenOfType method in the following way:
private
List<FieldRangeStart> GetAllMergeFields(RadDocument document)
{
List<FieldRangeStart> mergeFields =
new
List<FieldRangeStart>();
foreach
(FieldRangeStart fieldStart
in
document.EnumerateChildrenOfType<FieldRangeStart>())
{
if
(fieldStart.Field
is
MergeField)
{
mergeFields.Add(fieldStart);
}
}
return
mergeFields;
}
As FieldRangeStart document elements are inlines, you can use the MoveToInline method of DocumentPosition in the following way:
this
.radRichTextBox.Document.CaretPosition.MoveToInline(fieldRangeStart.FirstLayoutBox
as
InlineLayoutBox, 0);
If you want to select the whole MergeField, this can be done like this:
this
.radRichTextBox.Document.Selection.SelectAnnotationRange(fieldRangeStart);
If you have other questions, do not hesitate to contact us again. All the best,
Iva
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>
Gudrun
You can move a document position to the start of MergeField range like this:
documentPosition.MoveToInline(fieldRangeStart.FirstLayoutBox
as
InlineLayoutBox, 0);
You can then compare this position to any other position (the current caret position, too) using the <, > and = operators:
if
(
this
.editor.Document.CaretPosition > documentPosition)
{
MessageBox.Show(
"Current caret position is after the merge field start."
);
}
Iva
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>
You don't need to move the caret position. In fact, you should not move it.
You should create a new document position and navigate that one to the start of the merge field:
DocumentPosition documentPosition =
new
DocumentPosition(
this
.editor.Document);
documentPosition.MoveToInline(fieldRangeStart.FirstLayoutBox
as
InlineLayoutBox, 0);
Then, you can proceed to compare this position to the current caret position, which has not changed its place:
if
(
this
.editor.Document.CaretPosition > documentPosition)
{
MessageBox.Show(
"Current caret position is after the merge field start."
);
}
Iva Toteva
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>