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
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
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
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
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);
}
Hi Boby,
I am using this.lastEditor.Document = new RadDocument(); to clear the document.
Regards,
Jagadeesh S