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

Select a Merge field on Mouse click

5 Answers 63 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Jagadeesh
Top achievements
Rank 1
Jagadeesh asked on 31 Jul 2015, 10:28 AM

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

5 Answers, 1 is accepted

Sort by
0
Accepted
Tanya
Telerik team
answered on 05 Aug 2015, 08:27 AM
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
0
Jagadeesh
Top achievements
Rank 1
answered on 05 Aug 2015, 12:52 PM

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

0
Boby
Telerik team
answered on 10 Aug 2015, 11:22 AM
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
0
Jagadeesh
Top achievements
Rank 1
answered on 12 Aug 2015, 11:57 AM

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

0
Jagadeesh
Top achievements
Rank 1
answered on 12 Aug 2015, 12:04 PM

Hi Boby,

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

Regards,

Jagadeesh S

Tags
RichTextBox
Asked by
Jagadeesh
Top achievements
Rank 1
Answers by
Tanya
Telerik team
Jagadeesh
Top achievements
Rank 1
Boby
Telerik team
Share this question
or