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

Get Hyperlink from selected text

3 Answers 168 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Tracey
Top achievements
Rank 1
Tracey asked on 02 Sep 2011, 03:39 PM
Hi

How can I figure out whether the selected text in RadRichTextBox is a Hyperlink (and how do I get the Hyperlink information)?

Many Thanks
Tracey

3 Answers, 1 is accepted

Sort by
0
Iva Toteva
Telerik team
answered on 07 Sep 2011, 06:53 PM
Hello Tracey,

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

0
Tracey
Top achievements
Rank 1
answered on 12 Sep 2011, 10:58 AM
Hi Iva

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();
and ToDocument errors:
'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?) 
0
Iva Toteva
Telerik team
answered on 15 Sep 2011, 02:01 PM
Hello Tracey,

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.

Regards,
Iva
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Tags
RichTextBox
Asked by
Tracey
Top achievements
Rank 1
Answers by
Iva Toteva
Telerik team
Tracey
Top achievements
Rank 1
Share this question
or