2

I am trying to format a hyperlink in a Rich Text Box using the Rich Text Format. I can get basic formatting working thanks to this answer, for example making text bold. However I cannot get the RTF formatted hyperlink to work. I found an example of making an RTF link here. However, when I try to put this in the Rich Text Box as seen below, it causes my application to crash. Any suggestions as to what i'm missing here?

string my_hyperlink_text = @"{\field{\*\fldinst HYPERLINK \"http://www.google.com/\"}{\fldrslt Google}}"
        
if (rtbControl is RichTextBox & rtbControl.Name == "name_of_control")  // Making sure the control is a RichTextBox
       {
            RichTextBox rtb = rtbControl as RichTextBox;
            rtb.Rtf = my_hyperlink_text;
       }
Braxvan
  • 67
  • 14
  • 1
    If you just need to have single link (not other text there), use a LinkLabel. Otherwise, you may want to go for the *native way* for this: [Links with arbitrary text in a RichTextBox](https://www.codeproject.com/Articles/9196/Links-with-arbitrary-text-in-a-RichTextBox). It may appear *complicated*, to simply insert a link, but it's something you do once, then store the Custom Control for future uses. At this time, you're disrupting the RTF text, removing the Header and the first paragraph definition: your RTB is hurting. – Jimi Aug 10 '20 at 19:19
  • Do the Rich Text Boxes not support RTF formatted links? Or is the RTF I have above just incorrect? I don't see the point in going through the trouble of setting up the custom control when, as far as I understand, Rich Text Boxes should support RTF formatted hyperlinks. – Braxvan Aug 10 '20 at 20:30
  • This is not crashing for me: `richTextBox1.Rtf = "{\\rtf1\field{\\*\\fldinst HYPERLINK \"http://www.google.com/\"}{\\fldrslt Google}}";` – insane_developer Aug 18 '20 at 00:10
  • While it doesn't cause the application to crash, using the above RTF, the rich text box displays the following string: "ield[][]HYPERLINK "http://www.google.com/"[][] Google". Not quite what i'm looking for. – Braxvan Aug 18 '20 at 17:17

1 Answers1

1

An easy way for getting rtfs to work is to write your text in Microsoft word, copy & paste it to Wordpad and the saving it as a RTF from there. The detour with MS Word is needed, because WordPad does not support entering links in the UI, although it handles them correctly when they come from other sources, like the clipboard. Also, MS Word creates massively bloated rtf.

The rtf file you create this way can then be opened in any text editor and can be used as a string constant in your program.

In your case, I suppose that the prefix and maybe the color table are missing and are causing the problem.

By the way: Wordpad is not much more than a wrapper around the Windows rtf control, i.e. the same control that you are using in your code.

Heinz Kessler
  • 1,610
  • 11
  • 24
  • This is pretty cool. What an easy way to generate RTF text templates. The only problem is that doing it this was prevents anything from being opened using Process.Start(e.LinkText) in a LinkClicked event since the LinkText must be a link itself. So if you use RTF to display "Google" as a hyperlink, the e.LinkText will be "Google" instead of "www.google.com" I think this can be solved using a global variable maybe to store the actual link and use it with Process.Start() and the LinkClicked event. Regardless, I think this answered my main question with formatting RTF text. Thank you! – Braxvan Aug 19 '20 at 15:33
  • 1
    I'm not sure but I think I had the same problem. In my case I used a dictionary with link texts and URLs, so that the rtf itself did not contain the target urls, but these were rather determined by the values in the dictionary. – Heinz Kessler Aug 19 '20 at 18:19