Hyperlink deleted event

6 posts, 0 answers
  1. Mas
    Mas avatar
    6 posts
    Member since:
    Sep 2018

    Posted 18 Dec 2018 Link to this post

    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 

     

     

     

  2. Dimitar
    Admin
    Dimitar avatar
    9 posts

    Posted 19 Dec 2018 Link to this post

    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.
  3. Mas
    Mas avatar
    6 posts
    Member since:
    Sep 2018

    Posted 24 Dec 2018 in reply to Dimitar Link to this post

    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 ?

     

  4. Dess | Tech Support Engineer, Sr.
    Admin
    Dess | Tech Support Engineer, Sr.  avatar
    3465 posts

    Posted 25 Dec 2018 Link to this post

    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.
  5. Mas
    Mas avatar
    6 posts
    Member since:
    Sep 2018

    Posted 22 Jan in reply to Dess | Tech Support Engineer, Sr. Link to this post

    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 ?

     

  6. Tanya
    Admin
    Tanya avatar
    857 posts

    Posted 25 Jan Link to this post

    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 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.
Back to Top