Change link color in RichTextBox - c#

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?

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

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

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

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

Related

C# How do you set richtextbox text to default?

When some one puts a highlighted sentence to my richtextbox.
For example,
I want to make them type default text if they want to. Let's say if user clicks a button and want it to make default text. How do I do that?
I have tried
Font arialFont = new Font("Arial", 10, FontStyle.Regular);
richTextBox1.SelectAll();
richTextBox1.SelectionFont = arialFont;
richTextBox1.BackColor = Color.White; // i also tried Color.Transparent but it only return error message "Control does not support transparent background colors"
// RichTextBox's property says that Appearance - BackColor - Window, but it didn't even exist on Color.~
How can I set text all in default state in RichTextBox?
I'm sorry for unclear question.
when a user copies a text that is hightlighted text like above picture. And if they keep type anything, the text is highlighted too. But I don't want that to happen.
So I want to make a button to clear all text and make them to type a default text(no highlight, bold, italic, etc...)
I hope I explained my problem well this time. Sorry for unclear question.
Thank you LarsTech, sorry for my poor English. But I kind of solved the problem by doing richTextBox1.SelectionBackColor = Color.Transparent;
I didn't know about Selection has a lot of different function. Probably from next time, I need to look for more functions that's already in it.
Thank you for the hint and have a nice day!

Is it possible to make text centered in richtextbox C# [duplicate]

How do I align the text in a RichTextBox?
Basically, the RTB contains:
"--testing"
"--TESTING"
"TESTING--"
"testing--"
Which all have the same number of characters, but have different alignments. How can I align them properly? Im fairly new to C# and confused since it aligned properly in Java's TextArea.
Thank you!
You want to use the RichTextBox.SelectionAlignment property.
For instance if you want the whole textbox centered, then you would do:
richTextBox1.SelectAll();
richTextBox1.SelectionAlignment = HorizontalAlignment.Center;
If you want only part of the textbox with a certain alignment, then use the RichTextBox.Select() routine to select the text, then set the SelectionAlignment property.
You would have to change the font to a monospaced font, like Courier. This behavior you're showing is standard with most fonts, as not all characters are the same width.
richTextBox1.SelectAll();
richTextBox1.SelectionAlignment = HorizontalAlignment.Center;
richTextBox1.DeselectAll();
Unless it is very necessary for you to use a rich textbox, you can simply use a textbox and choose alignment as
textbox.TextAlign = HorizontalAlignment.Center;/*could be left, right or center*/
RichTextBox1.SelectionAlignment = HorizontalAlignment.Center;
or
GetRichTextBox().SelectionAlignment = HorizontalAlignment.Center;
for multiple pages.

How do I highlight a certain word in a label?

I have been watching a couple of videos and I've noticed that you can highlight or set the BackColor for every word it finds in a RichTextBox, I tried doing this with a Label which is what I'm working with on my project but I can't use Label.Find and Label.SelectionBackColor etc. Is there a way I could search a word in my label and highlight it?
You cannot use two different foreground/background colors in a label. You might split up the text in different labels or just use a richtextbox.
Here is a workaround, create a RichTextBox and use it as a label.
Set these properties to make it look like a label:
richTextBox.ReadOnly = true;
richTextBox.BorderStyle = BorderStyle.None;
richTextBox.BackColor = SystemColors.Control; // or whatever your background color is
work around to disable User selection:
richTextBox.Enabled = false;
richTextBox.SelectAll();
richTextBox.SelectionColor = SystemColors.ControlText; // or whatever you want the default text color to be
// you have to set the color or else it will be gray because of Enabled=false
Edit: i just tried it, after SelectAll(); and SelectionColor = SystemColors.ControlText any changing or adding of Text keeps it black (unless the current SelectionStart is at a point of the text where the color is different

How to 'align' text in RichTextBox C#?

How do I align the text in a RichTextBox?
Basically, the RTB contains:
"--testing"
"--TESTING"
"TESTING--"
"testing--"
Which all have the same number of characters, but have different alignments. How can I align them properly? Im fairly new to C# and confused since it aligned properly in Java's TextArea.
Thank you!
You want to use the RichTextBox.SelectionAlignment property.
For instance if you want the whole textbox centered, then you would do:
richTextBox1.SelectAll();
richTextBox1.SelectionAlignment = HorizontalAlignment.Center;
If you want only part of the textbox with a certain alignment, then use the RichTextBox.Select() routine to select the text, then set the SelectionAlignment property.
You would have to change the font to a monospaced font, like Courier. This behavior you're showing is standard with most fonts, as not all characters are the same width.
richTextBox1.SelectAll();
richTextBox1.SelectionAlignment = HorizontalAlignment.Center;
richTextBox1.DeselectAll();
Unless it is very necessary for you to use a rich textbox, you can simply use a textbox and choose alignment as
textbox.TextAlign = HorizontalAlignment.Center;/*could be left, right or center*/
RichTextBox1.SelectionAlignment = HorizontalAlignment.Center;
or
GetRichTextBox().SelectionAlignment = HorizontalAlignment.Center;
for multiple pages.

White font with black border in XAML

as the title says, I would like to have a black font with a white border
in my TextBox.
How could it be achieved?
Cheers.
It's possible to convert text into geometry objects with advanced formatting like outlines and fills for individual letters - you can find out more about how to do that here.
Having said that, I'm not sure that this is possible inside a TextBox. It may be for read-only text (TextBlock) only.
Edit
This blog post shows some advanced font rendering techniques inside a TextBox. It may be handy for you.
What I always do for that is set the font color to white and set a DropShadow effect on the text.
Next, play around with depth, angle (set to 0 or something) and blur and you should be able to get what you want.
Check out Blacklight controls # http://blacklight.codeplex.com/. In particular look at the sample on http://mightymeaty.members.winisp.net/blacklight.silverlight/ | Visual Controls | TEXT | Stroke Text Block (Alpha).
A full class and example using Geometries:
http://blogs.msdn.com/b/wpfsdk/archive/2006/12/24/using-text-as-a-decorative-graphic.aspx

Categories