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

Hyperlink deleted event

5 Answers 68 Views
RibbonBar
This is a migrated thread and some comments may be shown as answers.
Mas
Top achievements
Rank 1
Mas asked on 18 Dec 2018, 11:50 AM

Hello All,

I have customized InsertHyperlinkDialog to insert hyperlink onto my RadRichTextEditor. My custom dialog insert the hyperlink and also create list of all links saved into a database table.

Now I wonder, can I detect and event when a link is deleted from my document to make me clear out this link from my list ? As long as I get the event, I have put a tag to make sure I can identify which link must be deleted from my list correctly.

Thanks 

 

 

 

5 Answers, 1 is accepted

Sort by
0
Dimitar
Telerik team
answered on 19 Dec 2018, 02:38 PM
Hello Mas,

You can detect when a hyperlink is deleted from your document using the following approach:

public partial class RadForm1 : Telerik.WinControls.UI.RadForm
{
    public RadForm1()
    {
        InitializeComponent();
 
        this.radRichTextEditor1.CommandExecuting += RadRichTextEditor1_CommandExecuting;
    }
 
    private void RadRichTextEditor1_CommandExecuting(object sender, Telerik.WinForms.Documents.RichTextBoxCommands.CommandExecutingEventArgs e)
    {
        if (e.Command is RemoveHyperlinkCommand ||
            e.Command is DeleteCommand)
        {
            HyperlinkRangeStart hyperlinkRangeStart = this.GetCurrentInnerFieldRangeStart();
            if (hyperlinkRangeStart != null)
            {
                string url = hyperlinkRangeStart.HyperlinkInfo.NavigateUri;
                Console.WriteLine(url);
            }
        }
    }
 
    protected HyperlinkRangeStart GetCurrentInnerFieldRangeStart()
    {
        InlineLayoutBox inlineBox = this.radRichTextEditor1.Document.CaretPosition.GetCurrentInlineBox();
        if (inlineBox == null)
        {
            return null;
        }
 
        return inlineBox.GetRootDocument().GetContainingAnnotationRanges<HyperlinkRangeStart>(inlineBox.AssociatedInline, true).FirstOrDefault();
    }
}

I hope you find this solution useful. Please don't hesitate to ask if you have further questions.

Regards,
Dimitar
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Mas
Top achievements
Rank 1
answered on 24 Dec 2018, 10:13 AM

Thanks for the reply, but I don't think this approach work well.

Using this approach, my deletion procedure called even if I only remove a character on the hyperlink, not all 'hyperlinked' character (character with underline and blue color).

Usually, user will expect that removing not all characters on a hyperlink does not remove the hyperlink itself but only to modify the link to click.

I wonder if I can detected whole link deletion by detecting HyperlinkRangeEnd ? Or any other 'event' fired when the link is totally removed from document ?

 

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 25 Dec 2018, 09:34 AM
Hello, Mas,     

By design, the hyperlink is removed when you delete a single character from the specific URL. Although a part of the URL may be remaining, it represents a plain text and it won't navigate you anywhere. A RemoveHyperlinkCommand is executed when you right click over a hyperlink and select the Remove Hyperlink menu item. This can be handled by the previously provided code snippet by my colleague, Dimitar.

However, if you simply click Backspace at the end of the hyperlink, you can detect the HyperlinkRangeEnd and extract the URL as follows:

private void radRichTextEditor1_CommandExecuting(object sender, Telerik.WinForms.Documents.RichTextBoxCommands.CommandExecutingEventArgs e)
{
    if (e.Command is RemoveHyperlinkCommand ||
        e.Command is DeleteCommand)
    {
        HyperlinkRangeStart hyperlinkRangeStart = this.GetCurrentInnerFieldRangeStart();
        if (hyperlinkRangeStart != null)
        {
            string url = hyperlinkRangeStart.HyperlinkInfo.NavigateUri;
            Console.WriteLine(url);
        }
        else
        {
            InlineLayoutBox previousLayoutBox = this.radRichTextEditor1.Document.CaretPosition.GetPreviousInlineBox();
            HyperlinkRangeEnd hyperlinkRangeEnd = previousLayoutBox.AssociatedDocumentElement as HyperlinkRangeEnd;
            if (hyperlinkRangeEnd != null)
            {
                hyperlinkRangeStart = hyperlinkRangeEnd.Start as HyperlinkRangeStart;
                string url = hyperlinkRangeStart.HyperlinkInfo.NavigateUri;
                Console.WriteLine(url);
            }
        }
    }
}
 
protected HyperlinkRangeStart GetCurrentInnerFieldRangeStart()
{
    InlineLayoutBox inlineBox = this.radRichTextEditor1.Document.CaretPosition.GetCurrentInlineBox();
    if (inlineBox == null)
    {
        return null;
    }
 
    return inlineBox.GetRootDocument().GetContainingAnnotationRanges<HyperlinkRangeStart>(inlineBox.AssociatedInline, true).FirstOrDefault();
}

Note that the hyperlink is removed at this specific moment and the rest of the text is just a plain text. There is no specific event that can be handled when the whole text is deleted from an initially existing hyperlink. The CommandExecuting event is fired for each document's manipulation. Hence, you can use it to initially extract the URL and detect whether the document contains this URL.

I hope this information helps.

Merry Christmas 

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Mas
Top achievements
Rank 1
answered on 22 Jan 2019, 10:26 AM

Hi Dess,

I have apply your code into my application but I still have complain from my user. 

Lets say I have link like this: *thislink*

When user select all *thislink* and pres delete (or backspace), your code failed to detect the link deleted.

HyperlinkRangeStart hyperlinkRangeStart = this.GetCurrentInnerFieldRangeStart();

return null, failed to detect the link. 

HyperlinkRangeStart hyperlinkRangeStart = this.GetCurrentInnerFieldRangeStart();

also retur null. So, there is no way I can detect the deleted Hyperlink.

If user put the cursor inside the link, lets say: *this--cursor--link* and press delete / backspace, your code work like a charm. It also detect the link perfectly when user put the cursor after the link: *thislink*--cursor-- and press backspace.

The only problem is when user select all the hyperlink (the word: *thislink* and press delete). Once more, it also *does not* detect when user use right click on the link and click remove link from context menu.

Do you have any suggestion ?

 

0
Tanya
Telerik team
answered on 25 Jan 2019, 09:31 AM
Hi Mas,

Indeed, when a selection is created, it contains more elements and that is why the hyperlink marker cannot be obtained through the position. Here is how I modified the code so it can return the first HyperlinkRangeStart in the current selection:
protected HyperlinkRangeStart GetCurrentInnerFieldRangeStart()
{
    if (!this.radRichTextBox.Document.Selection.IsEmpty)
    {
        return this.radRichTextBox.Document.Selection.GetAnnotationMarkersOfType<HyperlinkRangeStart>().FirstOrDefault();
    }
 
    InlineLayoutBox inlineBox = this.radRichTextBox.Document.CaretPosition.GetCurrentInlineBox();
    if (inlineBox == null)
    {
        return null;
    }
 
    return inlineBox.GetRootDocument().GetContainingAnnotationRanges<HyperlinkRangeStart>(inlineBox.AssociatedInline, true).FirstOrDefault();
}

Please, note that the selection might contain several hyperlinks so you might need to slightly modify the code if you would like to handle this case as well.

Hope this is helpful.

Regards,
Tanya
Progress Telerik
Get quickly and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
RibbonBar
Asked by
Mas
Top achievements
Rank 1
Answers by
Dimitar
Telerik team
Mas
Top achievements
Rank 1
Dess | Tech Support Engineer, Principal
Telerik team
Tanya
Telerik team
Share this question
or