0

I am looking for a command that would allow me to insert a link to text in richtextBox.I made a program that processes the rss file from the page, and displays its contents. I would like that I could click the title (not that it will be "https://something.com", but the text with a hyperlink) that will take me to the website. It seemed to me that it would be very simple, but after searching half of the internet, unfortunately, I have not yet found the answer.I write this in visual studio, and the project is a windows forms application.I would be very grateful if someone would help me.

Enigmativity
  • 113,464
  • 11
  • 89
  • 172
bli141
  • 1
  • You may need to use a [LinkLabel](https://learn.microsoft.com/en-us/dotnet/framework/winforms/controls/linklabel-control-windows-forms) control. – ikerbera Feb 20 '19 at 10:34
  • You can use `WebBrowser` control. – Reza Aghaei Feb 20 '19 at 10:38
  • `after searching half of the internet,` what did you search, what did you try? Did you see [this possibly duplicate question](https://stackoverflow.com/questions/9855292/links-inside-rich-textbox) or [How can I make a hyperlink work in a RichTextBox?](https://stackoverflow.com/questions/435607/how-can-i-make-a-hyperlink-work-in-a-richtextbox)? – Panagiotis Kanavos Feb 20 '19 at 10:46
  • But I do not want to add a link at the end of the text. I want the text to have a hyperlink in it. – bli141 Feb 20 '19 at 10:48
  • @bli141 add what you tried. The duplicates show how to handle link clicks, how to add links etc. Handling clicks is *one* question. Inserting links is another one though. – Panagiotis Kanavos Feb 20 '19 at 10:49
  • You need to set the the [DetectUrls](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.richtextbox.detecturls?view=netframework-4.7.2) property and handle the [LinkClicked](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.richtextbox.linkclicked?view=netframework-4.7.2) event if you want to handle link clicks. The RTF control isn't a browser, it can't follow links by itself – Panagiotis Kanavos Feb 20 '19 at 10:50
  • Also check [What is the RTF syntax for a hyperlink?](https://stackoverflow.com/questions/2850575/what-is-the-rtf-syntax-for-a-hyperlink) You can modify the RTF text itself through the [RichTextBox.Rtf](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.richtextbox.rtf?view=netframework-4.7.2) property and inject the hyperlink – Panagiotis Kanavos Feb 20 '19 at 10:52
  • Take a look at [this post](https://stackoverflow.com/a/39713817/3110834) to see how easily you can craete HTML document using model based T4 templates. It's really good and easy for HTML rendering of a C# model and show the result in `WebBrowser`. – Reza Aghaei Feb 20 '19 at 10:54
  • @RezaAghaei this is a question about *RTF*, not HTML. As for *that* question using HTML on a text printer like that found on cash registers would never work. There are no fonts or styles, only text and escape sequences – Panagiotis Kanavos Feb 20 '19 at 10:55
  • I see @PanagiotisKanavos and the *RTF* question has been answered in the [link](https://stackoverflow.com/questions/2850575/what-is-the-rtf-syntax-for-a-hyperlink) which you shared. However, I'm suggesting a much cleaner way for satisfying the requirement. Avoiding `RichTextBox` in this case and using `WebBrowserControl`. Unless the `RichTextBox` is an unchangeable part of the problem. – Reza Aghaei Feb 20 '19 at 11:02
  • @RezaAghaei how can you edit text with a *web browser* ? – Panagiotis Kanavos Feb 20 '19 at 11:12
  • @PanagiotisKanavos `
    Edit this text.
    `. It's an easily editable div. It's basically the way that online html editors work. The same way you can make the content of the `WebBrowser` control editable.
    – Reza Aghaei Feb 20 '19 at 11:22
  • However, it's up to the OP to choose the solution, I'm not saying never use a `RichTextBox`. But since a lot of developers are not aware of what they can do using `WebBrowser` control in their windows forms applications, I decided to share this option for their information. – Reza Aghaei Feb 20 '19 at 11:28

1 Answers1

0
const string font = @"{\fonttbl{\f0\fswiss\fprq2\fcharset0 Arial;}{\f1\fnil\fcharset0 Arial;}}";
const string fieldHyper = @"{\*\generator Riched20 10.0.19041}\viewkind4\uc1\pard\widctlpar\sa200\sl276\slmult1 {\f0\fs24\lang11274{\field{\*\fldinst{HYPERLINK " + "\"";
const string fieldFrienName = @"}}{\fldrslt{\ul\cf1\cf1\ul ";
const string closeFields = "}}}";
string friendlyName;
string hyperLink;

// I use a form to create the hyperlinks either URLs or files 
// on the local hard drive that takes care of the validations 
// and give the corresponding formatting to the strings.
// When they are files, the path string must have the following format
// c:\\\\useful\\\\instructive.pdf 

if (yourRichTextBox.SelectionLength != 0)
{
    yourRichTextBox.Rtf = yourRichTextBox.Rtf.Replace(yourRichTextBox.SelectedText, "@@@");
}
else
{
    yourRichTextBox.SelectionLength = 0;
    yourRichTextBox.SelectedText = "@@@";
}

friendlyName = "Google engine";
hyperLink = "https://www.google.com";
//hyperlink = @"c:\\\\useful\\\\instructive.pdf";

StringBuilder link = new StringBuilder();
link.Append(font);
link.Append(fieldHyper);
link.Append(hyperLink + "\"");
link.Append(fieldFrienName);
link.Append(friendlyName);
link.Append(closeFields);
yourRichTextBox.Rtf = yourRichTextBox.Rtf.Replace("@@@", link.ToString());

//I hope it helps you... 
Miguel
  • 1
  • 1