I'm making a syntax analizer, so what I do is to change the colors and fonts of some words in a RichTextBox, but sometimes, when the text is too long, my richtextbox shows some highlighting. I want to change these to the same BackColor and ForeColor as the current word properties, so user can't notice this highlighting. How can I change the RichTextBox highlighted word Back and Fore colors ?
Asked
Active
Viewed 1,330 times
1
-
See [How to append text to RichTextBox without scrolling and losing selection?](http://stackoverflow.com/a/6550415/719186) – LarsTech Sep 04 '15 at 23:48
-
@LarsTech Thanks! That's what I was asking for :)) Working fine :P – RAS Sep 05 '15 at 00:02
2 Answers
1
If you want to change color and back color of selected text, try this (If I understood your question correctly)
this.richTextBox1.SelectionColor = Color.Red;
this.richTextBox1.SelectionBackColor = Color.Blue;

Reza Aghaei
- 120,393
- 18
- 203
- 398
1
If you want to set all of the text in the richtextbox
then type
this.richtextbox.SelectAll();
And then follow up with
this.richTextBox1.SelectionColor = Color.Red;
this.richTextBox1.SelectionBackColor = Color.Blue;
As Reza Aghaei said.
If you want it do this automatically then double click the richtextbox to create the text changed event and put the code inside this.
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
//Remember the cursor position & length
int SelectionStart = richTextBox1.SelectionStart;
int SelectionLength = richTextBox1.SelectionLength;
//Select all text and change color
richtextbox1.SelectAll();
richTextBox1.SelectionColor = Color.Red;
richTextBox1.SelectionBackColor = Color.Blue;
//Select original text
richTextBox1.Select(SelectionStart, SelectionLength);
}

Nicholas Ramsay
- 465
- 2
- 16