I'm trying to insert a hyperlink to a Rich Text Box in WinForms (using .NET Framework 4.6.1). The way I'm doing it is asking both the URL and text to be inserted, and afterwards appending it to the rtb (I know it might be better to do it with a selected text, then only asking for the URL).
But when I try to append the RTF it's as if it wasn't appended at all. The way I append it is the following:
string rtf = $"{{\\field{{\\*\\fldinst HYPERLINK \"{url}\"}}{{\\fldrslt {urlText}}}}}";
richTb.Rtf += rtf;
I've also tried appending to richTb.SelectionRtf
instead, but I got the same result. Am I missing something??
For WinForms you can try to use LinkLabel
LinkLabel link = new LinkLabel();
link.Text = "Microsoft";
LinkLabel.Link data = new LinkLabel.Link();
data.LinkData = "https://www.microsoft.com/";
link.Links.Add(data);
link.Location = richTextBox1.GetPositionFromCharIndex(richTextBox1.TextLength);
richTextBox1.Controls.Add(link);
For WPF you have to insert hyperlinks like Hyperlink not like text:
Paragraph parx = new Paragraph();
Run run1 = new Run("Text preceeding the hyperlink.");
Run run2 = new Run("Text following the hyperlink.");
Run run3 = new Run("Link Text.");
Hyperlink hyperl = new Hyperlink(run3);
hyperl.NavigateUri = new Uri("http://search.msn.com");
parx.Inlines.Add(run1);
parx.Inlines.Add(hyperl);
parx.Inlines.Add(run2);
Adding a paragraph to the RichTextBox should look like
richTextBox1.IsDocumentEnabled = true;
richTextBox1.Document.Blocks.Add(parx);