【问题标题】:Insert a link to text in a RichTextBox在 RichTextBox 中插入指向文本的链接
【发布时间】:2019-07-14 00:20:19
【问题描述】:

我正在寻找一个可以让我在richtextBox 中插入文本链接的命令。我制作了一个程序来处理页面中的rss 文件并显示其内容。我希望我可以单击标题(不是“https://something.com”,而是带有超链接的文本)将我带到该网站。 在我看来这会很简单,但是在网上搜索了一半之后,不幸的是,我还没有找到答案。我在visual studio中写了这个,项目是一个windows forms应用程序。我将不胜感激如果有人愿意帮助我。

【问题讨论】:

  • 您可能需要使用LinkLabel 控件。
  • 可以使用WebBrowser控制。
  • after searching half of the internet, 你搜索了什么,你尝试了什么?你看到this possibly duplicate questionHow can I make a hyperlink work in a RichTextBox?了吗?
  • 但我不想在文本末尾添加链接。我希望文本中有一个超链接。
  • @bli141 添加您尝试过的内容。副本显示了如何处理链接点击、如何添加链接等。处理点击是一个的问题。插入链接是另一种方式。

标签: c# visual-studio winforms


【解决方案1】:
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... 

【讨论】:

    猜你喜欢
    • 2014-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多