Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6851443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:14:54+00:00 2026-05-27T01:14:54+00:00

I have a RichTextBox that the user can edit to create a hyperlink (in

  • 0

I have a RichTextBox that the user can edit to create a hyperlink (in my case to another page with the document rather than an external URL). Having successfully created the link I now need to be able to remove it.

I have code that identifies that I’ve got a hyperlink in the current selection:

TextSelection linkText = richTextBox.Selection;

if (linkText != null && !string.IsNullOrWhiteSpace(linkText.Text))
{
    XElement root = XElement.Parse(linkText.Xaml);
    XNamespace ns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation";
    XElement linkElement = root.Element(ns + "Paragraph").Element(ns + "Hyperlink");
    if (linkElement != null)
    {
       // Get here if have a Hyperlink. How do I remove or update?
    }
}

However, I’m now stuck on the bit that goes inside the if test. How do I find the hyperlink so I can remove it completely?

My code for setting up the hyperlink is:

TextSelection linkText = richTextBox.Selection;
var hyperlink = new Hyperlink();

hyperlink.Inlines.Add(linkText.Text);
if (!String.IsNullOrEmpty(selectedTopic))  // A string holding the link target
{
    // Setup hyperlink here
}

linkText.Insert(hyperlink);

I’ve managed to work out how to update the hyperlink:

foreach (var block in richTextBox.Blocks)
{
    Paragraph p = block as Paragraph;

    foreach (var inline in p.Inlines)
    {
        var hyperlink = inline as Hyperlink;
        if (hyperlink != null && hyperlink.NavigateUri.AbsoluteUri.Contains(currentLink))
        {
            hyperlink.NavigateUri = new Uri(newLink);
        }
    }
}

I could use the same approach to delete the hyperlink, but how do I convert the Hyperlink to a normal Inline?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T01:14:55+00:00Added an answer on May 27, 2026 at 1:14 am

    Looks like you are getting close you just need to hold a reference to the link then use Remove. Something like the following (I like to use Linq to make things a little more succinct):-

    foreach (var p in richTextBox.Blocks.OfType<Paragraph>()) 
    { 
        var hyperlink = p.Inlines.OfType<HyperLink>()
            .FirstOrDefault(hl => hl.NavigateUri.AbsoluteUri.Contains(currentLink));
    
        if (hyperlink != null)
        {
             p.Inlines.Remove(hyperlink);
             break;
        }
    } 
    

    Edit: Want to leave the content of hyperlink in place? (i.e., just remove the wrapping hyperlink),

    foreach (var p in richTextBox.Blocks.OfType<Paragraph>()) 
    { 
        var hyperlink = p.Inlines.OfType<HyperLink>()
            .FirstOrDefault(hl => hl.NavigateUri.AbsoluteUri.Contains(currentLink));
    
        if (hyperlink != null)
        {
             int index = p.Inlines.IndexOf(hyperlink);
             Span span = new Span();
    
             foreach (var inline in hyperlink.Inlines.ToArray())
             {
                 hyperlink.Inlines.Remove(inline);
                 span.Inlines.Add(inline);
             }
    
             // You may need code here to preserve the Font properties etc  from hyperlink to span.
    
             p.Inlines[index] = span;
             break;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a textbox that the user can input into. Right now the user
I have a RichTextBox that I write a string to every time I click
I have a Windows Forms application that on it I have a RichTextBox, like
I have a method that is meant to display output on a RichTextBox in
I've got a richtextbox, that I plan on saving to a database, which can
I have a RichTextBox that I'm spewing log information to, but the RichTextBox seems
I have a requirement to create an application that will open up about 10
I have a small WPF application that hosts a RichTextBox: <RichTextBox SpellCheck.IsEnabled=True FontFamily=Verdana AcceptsTab=True
I have a richtextbox that its text is a concatenation of some words from
I have a WPF RichTextBox control that I want to behave like a normal

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.