可能重复:
How to select text from the RichTextBox and then color it?
我真的没有要显示的代码,因为我不知道:(。我有一个服务器,用标签输出信息。例如:
15:44 [INFO] Loaded Properties
15:45 [ERROR] Properties not found
如何查看richtextbox并将所有 ERROR 标记设为红色, INFO 标记为绿色等?
答案 0 :(得分:2)
我认为这应该做你想要的:
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;
}
修改:将StartsWith
更改为Contains
答案 1 :(得分:0)
您可以执行以下操作:
//will select characters form index 0 to 9
richTextBox1.Select(0, 10);
//will set the characters from 0 to 9 to red
richTextBox1.SelectionColor = Color.Red;