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

Find position to MergFields

5 Answers 120 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Gudrun
Top achievements
Rank 1
Gudrun asked on 10 Aug 2011, 01:22 PM
Hi,

Is there a way to search for Mergefields in a Document and get the DocumentPosition? 

Gudrun

5 Answers, 1 is accepted

Sort by
0
Iva Toteva
Telerik team
answered on 15 Aug 2011, 03:17 PM
Hello Gudrun,

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);
In this way, the caret (or another DocumentPosition you have defined) will be moved just before the start of the MergeField.

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

0
Gudrun
Top achievements
Rank 1
answered on 07 Oct 2011, 08:15 AM
Thanks for the reply, i would also know if it's possible to find a mergeField position in relation to another mergeField/caret position.. 

Gudrun
0
Accepted
Iva Toteva
Telerik team
answered on 11 Oct 2011, 07:55 PM
Hello 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.");
}

Greetings,
Iva
the Telerik team

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

0
Gudrun
Top achievements
Rank 1
answered on 01 Nov 2011, 12:46 PM
Is it possible to do this without moving the caret? When I do this it happens that the caret ends up in the wrong place in relation to the original position.

0
Iva Toteva
Telerik team
answered on 04 Nov 2011, 11:40 AM
Hi Gudrun,

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.");
}

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
Gudrun
Top achievements
Rank 1
Answers by
Iva Toteva
Telerik team
Gudrun
Top achievements
Rank 1
Share this question
or