Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - RichTextBox change color of certain words [duplicate]

Tags:

c#

richtextbox

Possible Duplicate:
How to select text from the RichTextBox and then color it?

I don't really have any code to show, because I don't know :(. I have a server that outputs information with tags. For example:

15:44 [INFO] Loaded Properties
15:45 [ERROR] Properties not found

How do I look in the richtextbox and make all ERROR tags red, INFO tags GREEN, etc.?

like image 618
Alex Ogden Avatar asked Nov 05 '22 02:11

Alex Ogden


1 Answers

I think this should do what you want:

for(int i=0; i<rtb.Lines.Length; i++) 
{ 
   string text = rtb.Lines[i];
   rtb.Select(rtb.GetFirstCharIndexFromLine(i), text.Length); 
   rtb.SelectionColor = colorForLine(text); 
} 

private Color colorForLine(string line)
{
    if(line.Contains("[INFO]", StringComparison.InvariantCultureIgnoreCase) return Color.Green;
    if(line.Contains("[ERROR]", StringComparison.InvariantCultureIgnoreCase) return Color.Red;

    return Color.Black;
}

Edit: Changed StartsWith to Contains

like image 60
aKzenT Avatar answered Nov 09 '22 04:11

aKzenT