How can I make a hyperlink work in a RichTextBox?

C#WinformsRichtextbox

C# Problem Overview


When I add www.stackoverflow.com into my RichTextBox and run the program it is shown in blue and as a hyperlink yet when I click it nothing happens. How can I fix this?

C# Solutions


Solution 1 - C#

  1. Make sure the text property includes a valid url. E.g. http://www.stackoverflow.com/

  2. set the DetectUrls property to true

  3. Write an event handler for the LinkClicked event.

Personally, I wouldn't pass "IExplore.exe" in as a parameter to the Process.Start call as Microsoft advise as this presupposes that it is installed, and is the user's preferred browser. If you just pass the url to process start (as per below) then Windows will do the right thing and fire up the user's preferred browser with the appropriate url.

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

Solution 2 - C#

RichTextBox class allows you to customize its behavior when user clicks the hyperlink. Add an event handler for the RichTextBox.LinkClicked event

Process p = new Process();

private void richTextBox1_LinkClicked(object sender, LinkClickedEventArgs e)
{
   p = Process.Start("IExplore.exe", e.LinkText);
}

Solution 3 - C#

You should make sure that DetectUrls is set to true. If that doesn't work on its own, you may need to add a handler for the LinkClicked event.

Solution 4 - C#

Is yourTextBox.DetectUrls set to true? We may need some more info to provide a better answer.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJade MView Question on Stackoverflow
Solution 1 - C#Sam MeldrumView Answer on Stackoverflow
Solution 2 - C#akuView Answer on Stackoverflow
Solution 3 - C#Jeff YatesView Answer on Stackoverflow
Solution 4 - C#Andrew HareView Answer on Stackoverflow