Click here to Skip to main content
15,881,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my project I have richtextbox. On selecting particular text default blue color appears on the selected text. I want to change that blue color to some other color.

How can i change?
Updated 29-Oct-11 3:27am
Sergey Alexandrovich Kryukov 30-Oct-11 0:55am    
What rich box type do you use? There is more then one. Please, full type name.

1 solution

Note: I've chosen to answer this question assuming you are talking about setting the background color of selected text: to set the text foreground color, just substitute 'SelectionColor' in the examples below for 'SelectionBackColor.'

Assuming a WinForms RichTextBox control named 'richTextBox1: you can modify, at run-time, the background color of any currently selected text like this:
richTextBox1.SelectionBackColor = Color.Gold;
RichTextBox 'SelectionBackColor' is a Property with both 'set and 'get methods: so you can analyze what color the current background color is, and do various things based on the result of your analysis, if you wish.

And, of course you can manipulate the background color of the RTF text programmatically by setting your own selection:
richTextBox1.Text = @"line1

richTextBox1.SelectionStart = 0;
richTextBox1.SelectionLength = 5;
richTextBox1.SelectionBackColor = Color.LightSkyBlue;
What happens if your selected text in the RichTextBox contains more than one color ? May I suggest you explore, in that case, what the value returned by 'SelectionBackColor' will be.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900