在RichTextBox中插入文本链接

时间:2019-02-20 10:31:28

标签: c# visual-studio winforms

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

1 个答案:

答案 0 :(得分: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... 
相关问题