Phoexo
Phoexo

Reputation: 2556

Change link color in RichTextBox

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?

Upvotes: 1

Views: 4932

Answers (4)

Ehz
Ehz

Reputation: 2067

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

Upvotes: 0

Robert Harvey
Robert Harvey

Reputation: 180858

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

Upvotes: 3

Robert Harvey
Robert Harvey

Reputation: 180858

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

Upvotes: 2

Scott Ferguson
Scott Ferguson

Reputation: 7830

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).

Upvotes: 1

Related Questions