Select a Merge field on Mouse click

6 posts, 1 answers
  1. Jagadeesh
    Jagadeesh avatar
    4 posts
    Member since:
    Jul 2015

    Posted 31 Jul 2015 Link to this post

    Hi,

    Is there an option to select a merge field on mouse click ?

    I have tried with the option specified in "http://www.telerik.com/forums/find-position-to-mergfields " but facing issues when there is a same merge field placed twice in a document.

    Jagadeesh

  2. Answer
    Tanya
    Admin
    Tanya avatar
    404 posts

    Posted 05 Aug 2015 Link to this post

    Hello Jagadeesh,

    You could subscribe to the MouseLeftButtonDown event of the RadRichTextBox and check if the position is inside a MergeField. If this is true you can select the current annotation range using the SelectAnnotationRange() method of the DocumentSelection. The snippet below demonstrates how this approach could be implemented:
    void radRichTextBox_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        DocumentPosition position = this.radRichTextBox.ActiveEditorPresenter.GetDocumentPositionFromViewPoint(e.GetPosition(this.radRichTextBox));
        FieldRangeStart field = this.radRichTextBox.Document.GetContainingAnnotationRanges<FieldRangeStart>(position.GetCurrentInline(), f => f.Field is MergeField).FirstOrDefault();
     
        if (field != null)
        {
            this.radRichTextBox.Document.Selection.SelectAnnotationRange(field);
            e.Handled = true;
        }
    }

    Hope this is helpful.

    Regards,
    Tanya
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. DevCraft banner
  4. Jagadeesh
    Jagadeesh avatar
    4 posts
    Member since:
    Jul 2015

    Posted 05 Aug 2015 in reply to Tanya Link to this post

    Hello Tanya,

         Thanks for your reply, its working fine when I place a merge field inside a body content, whereas if my merge field is placed inside a header or footer, its not working, MouseLeftButtonDown event is not getting fired when I click inside a header or a footer.

     Jagadeesh S

  5. Boby
    Admin
    Boby avatar
    595 posts

    Posted 10 Aug 2015 Link to this post

    Hi Jagadeesh,

    In this case, you can use the ActiveDocumentEditorChanged event and subscribe to the mouse events of the current editor. 

    Note that the control was not meant for such scenario and the code provided is more of a hack, and doesn't work consistently in some cases - namely it selects the field only every second time in headers/footers:

    public partial class Example : UserControl
    {
        private RadRichTextBox lastEditor;
        private MouseButtonEventHandler lastEventHandler;
      
        private void RadRichTextBox_ActiveDocumentEditorChanged(object sender, ActiveDocumentEditorChangedEventArgs e)
        {
            this.AttachEvents();
        }
      
        private void AttachEvents()
        {
            if (lastEditor != null)
            {
                this.lastEditor.RemoveHandler(UIElement.MouseLeftButtonDownEvent, lastEventHandler);
            }
      
            this.lastEditor = (RadRichTextBox)this.radRichTextBox.ActiveDocumentEditor;
            this.lastEventHandler = new MouseButtonEventHandler(LastEditor_MouseLeftButtonDown);
            this.lastEditor.AddHandler(UIElement.MouseLeftButtonDownEvent, this.lastEventHandler, true);
        }
      
        private void LastEditor_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            DocumentPosition position = this.lastEditor.ActiveEditorPresenter.GetDocumentPositionFromViewPoint(e.GetPosition(this.lastEditor));
      
            FieldRangeStart field = this.lastEditor.Document.GetContainingAnnotationRanges<FieldRangeStart>(position.GetCurrentInline(), f => f.Field is MergeField).FirstOrDefault();
            if (field != null)
            {
                this.lastEditor.Document.Selection.SelectAnnotationRange(field);
                e.Handled = true;
            }
        }
      
        public Example()
        {
            InitializeComponent();
      
            this.radRichTextBox.ActiveDocumentEditorChanged += this.RadRichTextBox_ActiveDocumentEditorChanged;
      
            this.AttachEvents();
        }
    }


    Regards,
    Boby
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  6. Jagadeesh
    Jagadeesh avatar
    4 posts
    Member since:
    Jul 2015

    Posted 12 Aug 2015 in reply to Boby Link to this post

    Hi Boby,

         Thanks for your reply, using the code snippet you have shared mouse click event got fired when I click inside the header, but when I am back to document content mouse click event was not getting fired. So modified the attached code as below and now its working fine with respect to mouse click. Whereas clearing of document (New Document creation) is not happening when the cursor is inside the header, header content alone is getting cleared.

    Modified AttachEvents as:

    private void AttachEvents()
    {
        if (lastEditor != null)
        {
            this.lastEditor.RemoveHandler(UIElement.MouseLeftButtonDownEvent, lastEventHandler);
        }
     
        if (sender != null)
            this.lastEditor = (RadRichTextBox)sender.ActiveDocumentEditor;
        else
            this.lastEditor = (RadRichTextBox)this.radRichTextBox.ActiveDocumentEditor;
             
        this.lastEventHandler = new MouseButtonEventHandler(LastEditor_MouseLeftButtonDown);
        this.lastEditor.AddHandler(UIElement.MouseLeftButtonDownEvent, this.lastEventHandler, true);
    }

  7. Jagadeesh
    Jagadeesh avatar
    4 posts
    Member since:
    Jul 2015

    Posted 12 Aug 2015 in reply to Boby Link to this post

    Hi Boby,

         I am using this.lastEditor.Document = new RadDocument();  to clear the document.

    Regards,

    Jagadeesh S

Back to Top
DevCraft banner