Reputation: 11
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 ?
Upvotes: 1
Views: 1599
Reputation: 475
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);
}
Upvotes: 2
Reputation: 125312
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;
Upvotes: 2