Find position to MergFields

6 posts, 1 answers
  1. Gudrun
    Gudrun avatar
    8 posts
    Member since:
    Dec 2010

    Posted 10 Aug 2011 Link to this post

    Hi,

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

    Gudrun
  2. Iva Toteva
    Admin
    Iva Toteva avatar
    1319 posts

    Posted 15 Aug 2011 Link to this post

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

  3. Gudrun
    Gudrun avatar
    8 posts
    Member since:
    Dec 2010

    Posted 07 Oct 2011 Link to this post

    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
  4. Answer
    Iva Toteva
    Admin
    Iva Toteva avatar
    1319 posts

    Posted 11 Oct 2011 Link to this post

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

  5. Gudrun
    Gudrun avatar
    8 posts
    Member since:
    Dec 2010

    Posted 01 Nov 2011 Link to this post

    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.

  6. Iva Toteva
    Admin
    Iva Toteva avatar
    1319 posts

    Posted 04 Nov 2011 Link to this post

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

Back to Top