Change link color in RichTextBox

孤街浪徒 提交于 2019-12-08 04:08:56

问题


I have a RichTextBox which contains links posted by the users.

The problem is that my RTB makes the color of the links black, and the background color is also black. This leads to the links being invisible.

How do I change the color of the links in the RTB?


回答1:


Phoexo:

Have a look at the following CodeProject article. This fellow provides a way to create arbitrary links in the text that work, while the DetectUrls property is set to false. With a small amount of hacking, you should have full control of the formatting of your links.

Links with arbitrary text in a RichTextBox
http://www.codeproject.com/KB/edit/RichTextBoxLinks.aspx?display=Print




回答2:


string str = richTextBox1.Text;

Regex re = new Regex("^((ht|f)tp(s?)\:\/\/|~/|/)?([\w]+:\w+@)?([a-zA-Z]{1}([\w\-]+\.)+([\w]{2,5}))(:[\d]{1,5})?((/?\w+/)+|/?)(\w+\.[\w]{3,4})?((\?\w+=\w+)?(&\w+=\w+)*)?", RegexOptions.None);

MatchCollection mc = re.Matches(str);

foreach (Match ma in mc)
{
    richTextBox1.Select(ma.Index, ma.Length);
    richTextBox1.SelectionColor = Color.Red;
}

http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvcs/thread/1f757f8c-427e-4042-8976-9ac4fd9caa22




回答3:


I'm not sure how to change the color of the links, but you can change the way that the RTB handles URLs.

Try setting the DetectUrls property to false.

That way, the link will be the same color as the RTB text, and visible. (Although not clickable).




回答4:


You could try changing the formatting in the RichText itself. The fonttbl keyword allows you to do text formats.

http://msdn.microsoft.com/en-us/library/aa140277(office.10).aspx



来源:https://stackoverflow.com/questions/1061696/change-link-color-in-richtextbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!