55

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?

Breeze
  • 2,010
  • 2
  • 32
  • 43
  • http://stackoverflow.com/questions/762271/clicking-hyperlinks-in-a-richtextbox-without-holding-down-ctrl-wpf is the best thread for this I found (so far) – CAD bloke Apr 18 '16 at 10:20

4 Answers4

139
  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);
}
Sam Meldrum
  • 13,835
  • 6
  • 33
  • 40
  • Is it possible for UNC, paths like E:\My Documents\Logs\log1.log ? – Kiquenet Oct 11 '12 at 10:16
  • 1
    @Kiquenet - not sure, but try using an entry of the form file:////e:/My%20Documents/Logs/log1.log – Sam Meldrum Oct 17 '12 at 12:33
  • 12
    This can be a grave security risk. A better way is: `System.Diagnostics.Process.Start("explorer.exe", e.LinkText);` – Christian Jun 13 '14 at 10:27
  • 2
    Just to reiterate the point @Christian is making -- **explorer.exe** passes the arguments to a singleton, which is guaranteed running un-elevated. Running the command yourself it may or may not be elevated. If explorer runs it un-elevated, and it needs elevation, the UAC will ask. Much safer. – Jesse Chisholm Oct 30 '14 at 15:13
  • I use this and the system throws the error ['The system cannot find the file specified' when clicking link](https://stackoverflow.com/q/74732645/3416774) – Ooker Dec 08 '22 at 15:29
10

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);
}
aku
  • 122,288
  • 32
  • 173
  • 203
2

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.

Jeff Yates
  • 61,417
  • 20
  • 137
  • 189
  • 1
    Unable to find DetectUrl property in RichTextBox – user2323308 May 19 '15 at 03:13
  • 1
    The DetectUrl property is only in the WinForms version of RichTextBox. The WPF version does not have this property. Try this link: http://stackoverflow.com/questions/762271/clicking-hyperlinks-in-a-richtextbox-without-holding-down-ctrl-wpf – Drew Dec 05 '16 at 18:06
1

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

Andrew Hare
  • 344,730
  • 71
  • 640
  • 635