views:

756

answers:

4

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  A: 

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

Scott Ferguson
+1  A: 
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

Robert Harvey
Yeah, I found that one, but I didn't get it to work; it did find the links, but the color remained the same.
Phoexo
Did you try setting the DetectUrls property to false, as Scott suggests? I'll bet you can change the color then.
Robert Harvey
No, I did not try that, but I still want the user to be able to click the link and be redirected to the browser.
Phoexo
+2  A: 

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

Robert Harvey
A: 

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

OG