DefColin DefColin - 19 days ago 11
C# Question

How to change color for specific string in richTextBox

I'm trying to change color for specific string into the

richTextBox
with opening of text document inside it to already existing string or added through the
richTextBox
to keep other text saved and black except specific line.

I got inside the
richTextBox_TextChanged
event, but does not works proper for me. It changes text for specific string well and keeps other text black, but in all cases I have same two problems, first:

If I add text to specific colored red string from
richTextBox
merged with other word or character, for example:

if text document content is:

some string
some string
red string


and if I add to it something like:

some string
some string
xred string


or:

some string
some string
red stringx


result becomes second, if I add another one string equal to
"red string"
:

some string
some string
red stringx // << This line remains red
red string // << and this does not changes and remains black


And another problem, if I write text after red string in
richTextBox
, all following text at the stage of writing also becomes red.

For example string which must be red is:

string Str = "red string";


this way:

Color aColor = Color.FromName(Str.Split(' ')[0]);
if (richTextBox1.Text.Contains(Str) && aColor != Color.Red)
{
richTextBox1.Select(richTextBox1.Text.IndexOf(Str), Str.Length);
richTextBox1.SelectionColor = Color.Red;
}


or this way:

Color aColor = Color.FromName(Str.Split(' ')[0]);
if (richTextBox1.Text.Contains(Str) && aColor != Color.Red)
{
richTextBox1.Find(Str);
richTextBox1.SelectionColor = Color.Red;
}


Or this way, which can contain listed strings to change colors for each different color if needed this way
string[] words = { "specword1", "specword2" };
, but in this case just shown as different way to do it, with same and only needed value from above, and empty
string[] words
:

string[] words = { "" };
Color[] colors = { Color.Red };
for (int i = 0; i < words.Length; i++)
{
string word = words[i];
Color color = colors[i];
{
richTextBox1.Find(Str);
richTextBox1.SelectionColor = color;
}
}


I got same two problems in result with all attempts, and if I use it with
Form1_Load
it just does not makes any changes to color.

So I'm wondering what can be solution for this case, and only one thing comes to mind, but not sure if it is proper way to solve this problems:

I'm not sure how, but somehow to make unavailable editing of red string, which is always is on separate line in text document, and at the same time do not allow to write or paste it by hand into the
richTextBox
.

Anyway if it can help with first problem, seems like it does not helps to avoid changing of color for following text after red string.

Answer

inside richTextBox_TextChanged check the full line of text when comparing

string str = "red string";
for(int i=0; i<richTextBox1.Lines.Length; i++) 
{ 
   string text = richTextBox1.Lines[i];
   if(text ==str)
   {
    richTextBox1.Select(richTextBox1.GetFirstCharIndexFromLine(i), text.Length); 
    richTextBox1.SelectionColor = Color.Red;
   }   
}

for multiple colors, I would use dictionary

var dictionary = new Dictionary<string, System.Drawing.Color>();
    dictionary.Add("red color", System.Drawing.Color.Red);
    dictionary.Add("Blue color", System.Drawing.Color.Blue);
//as above example you can use for loop and get each line of rich textbox
string linefromTextBox = "Blue color";
//then check that line contain of of text in the dictionaly 
if (dictionary.ContainsKey(linefromTextBox))
{
   // if key found then you can get the color as below
   // asign this as SelectionColor 
   //before that you need to Select the line from rich text box as above example
   var color = dictionary[linefromTextBox];
} 
Comments