How to get current Hyperlink, when Caret is located in a Hyperlink

4 posts, 0 answers
  1. kun
    kun avatar
    3 posts
    Member since:
    Jan 2016

    Posted 25 Jan Link to this post

    hello

    Dear Telerik Team

    We want to make: When caret enter into a Hyperlink, or delete a hyperlink, or press Backspace key to delete the Hyperlink, a popup is triggered. We used the method you provided to show a popup (use click), and was very successful.

    1.But now question is that we can not distinguish whether it is a Hyperlink, when caret enter into the hyperlink.

    2.Another question is that: how to programmly change the style of a Hyperlink.

     

    Thanks, We hope your help.

     

    Kun

  2. Tanya
    Admin
    Tanya avatar
    402 posts

    Posted 28 Jan Link to this post

    Hi Kun,

    You could detect if the caret is inside a hyperlink using the GetNextInline() method of the CaretPosition. From the Inline, you could extract if it is a Hyperlink using the following approach:
    Inline inline = this.radRichTextBox.Document.CaretPosition.GetCurrentInline();
     
    IHyperlinkRangeMarker hyperlink = inline.FieldStart as IHyperlinkRangeMarker;
    if (hyperlink != null)
    {
        // The caret position is inside a hyperlink
    }

    In order to change the behavior of the Backspace key, you could attach to the PreviewEditorKeyDown event of RadRichTextBox and execute the custom logic in the handler. There is an example of how to achieve this at the end of the Keyboard Support article.

    You can obtain and modify the default hyperlink style of the document through the StyleRepository property of RadDocument.
    StyleDefinition hyperlinkStyle = this.radRichTextBox.Document.StyleRepository[RadDocumentDefaultStyles.HyperlinkStyleName];

    More information about the styles in RadRichTextBox is available in our documentation on that matter.

    Hope this helps.

    Regards,
    Tanya
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. UI for WPF is Visual Studio 2017 Ready
  4. kun
    kun avatar
    3 posts
    Member since:
    Jan 2016

    Posted 28 Jan in reply to Tanya Link to this post

    Hi, Tanya

    We used codes you provided "StyleDefinition hyperlinkStyle =this.radRichTextBox.Document.StyleRepository[RadDocumentDefaultStyles.HyperlinkStyleName]; ",and refered to  RadDocument

    But we did not figure out how to define a custom style of Hyperlink.

    We used the codes as follow, but it did not take effect:

    StyleDefinition hyperlinkStyle =this.radRichTextBox.Document.StyleRepository[RadDocumentDefaultStyles.HyperlinkStyleName];

    StyleDefinition linkedParagraphStyle = new StyleDefinition();
                    linkedParagraphStyle.Type = StyleType.Paragraph;
                    linkedParagraphStyle.ParagraphProperties.Background = Colors.Yellow;
                    linkedParagraphStyle.DisplayName = "linkedParagraphStyle";
                    linkedParagraphStyle.Name = "linkedParagraphStyle";

                    StyleDefinition linkedCharStyle = new StyleDefinition();
                    linkedCharStyle.Type = StyleType.Character;
                    linkedCharStyle.SpanProperties.FontWeight = FontWeights.Bold;
                    linkedCharStyle.SpanProperties.FontSize = Unit.PointToDip(30);
                    linkedCharStyle.SpanProperties.ForeColor = Colors.Purple;
                    linkedCharStyle.DisplayName = "linkedCharStyle";
                    linkedCharStyle.Name = "linkedCharStyle";
                    linkedParagraphStyle.LinkedStyle = linkedCharStyle;

                    progressNotes.radRichTextBox.Document.StyleRepository.Add(linkedParagraphStyle);
                    progressNotes.radRichTextBox.Document.StyleRepository.Add(linkedCharStyle);

     

    We hope your help, thank you very much

  5. Tanya
    Admin
    Tanya avatar
    402 posts

    Posted 01 Feb Link to this post

    Hi Kun,

    You will need to create a new StyleDefinition object only if the goal is to create a new style. If I understand your requirement correctly, you need to modify the built-in style for hyperlinks. If so, you could obtain it through the StyleRepository of RadDocument and directly change its properties according to your preferences.

    Hope this makes things clear.

    Regards,
    Tanya
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top