3 Answers, 1 is accepted
There are several cases that you should handle depending on the exact behavior you are looking for.
First, RadRichTextBox supports multi-range selection, so depending on your scenario, you may have to distinguish between the cases when exactly one hyperlink is selected, a hyperlink is selected along with other text, there are several ranges in the selection and you need to search all of them for hyperilnks.
In the basic case, when you wish to determine if only one hyperlink is selected, the following method should do the trick:
private
bool
IsOnlyHyperlinkSelected(RadDocument document)
{
if
(
this
.radRichTextBox.Document.Selection.Ranges.Count != 1)
{
return
false
;
}
SelectionRange selectionRange =
this
.radRichTextBox.Document.Selection.Ranges.First;
DocumentPosition endPositionShifted =
new
DocumentPosition(selectionRange.EndPosition);
endPositionShifted.MoveToPrevious();
if
(selectionRange.StartPosition.GetCurrentInlineBox().AssociatedDocumentElement
is
HyperlinkRangeStart &&
endPositionShifted.GetCurrentInlineBox().AssociatedDocumentElement
is
HyperlinkRangeEnd)
{
return
true
;
//you can get the information of the hyperlink like this:
//HyperlinkRangeStart hyperlink = selectionRange.StartPosition.GetCurrentInlineBox().AssociatedDocumentElement as HyperlinkRangeStart;
//MessageBox.Show(hyperlink.HyperlinkInfo.NavigateUri);
}
return
false
;
}
If you want to get all Hyperlinks in the selection, you can do this like this:
RadDocument document =
this
.radRichTextBox.Document.Selection.CopySelectedDocumentElements().ToDocument();
foreach
(HyperlinkRangeStart hyperlink
in
document.EnumerateChildrenOfType<HyperlinkRangeStart>())
{
MessageBox.Show(hyperlink.HyperlinkInfo.NavigateUri);
}
I hope this helps. Regards,
Iva
the Telerik team
Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>
Thanks for this.
I am trying the IsOnlyHyperlinkSelected() function and it doesn't always return true when it should.
For example I select a link - open the edit hyperlink dialog (to make sure it is a link) - the link info is there, I then call IsOnlyHyperlinkSelected() with link still selected and it only returns true occassionally - most times it returns false. Any ideas why this is?
Also I try this
RadDocument document = this.radRichTextBox.Document.Selection.CopySelectedDocumentElements().ToDocument();
'Telerik.Windows.Documents.Model.DocumentFragment' does not contain a definition for 'toDocument' and no extension method 'toDocument' accepting a first argument of type 'Telerik.Windows.Documents.Model.DocumentFragment' could be found (are you missing a using directive or an assembly reference?)
I was not able to reproduce the issue with the method returning different values. However, the ShowInsertHyperlinkDialogCommand clears the selection after execution, so you would need to manually select the hyperlink. In that case, you might have accidentally selected the interval after the hyperlink, which is also selected on double click or triple click.
As for the ToDocument() method of DocumentFragment, it was introduced in the latest official release - Q2 2011 (2011.2 712), which you have marked as the version of controls you are using.
If you need further assistance, please elaborate more on your scenario, the steps to reproduce the case when the method from the snippet does not work and any additional information which will be helpful for resolving the issue.
Iva
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>