Pages Menu
TwitterRssFacebook
Categories Menu

Posted by on 17th March, 2011

Hyperlink in RichTextBox

Hyperlink in RichTextBox

Today I learned that the default C# RichTextBox control does not handle hyperlinks out of the box. Wait, it doesn’t..? How is that possible? Yes, hard to believe. So, two hours later, I found a workaround: if you do want to have hyperlinks work, you need to do the following:

First, set the DetectUrls property on the RichTextBox control to True.

Then, open the Designer.cs file and find the generated code block for the RichTextBlock properties and handlers.

Add the following line:

Add RichTextBlock handler

this.myRichTextBox.LinkClicked +=
    new LinkClickedEventHandler(this.myRichTextBox_LinkClicked);

Add the Eventhandler method

Add RichTextBlock handler method

private void myRichTextBox_LinkClicked(object sender, LinkClickedEventArgs e)
{
    system.Diagnostics.Process.Start(e.LinkText);
}

That is it…