C# - RichTextBox 更改某些单词的颜色 [英] C# - RichTextBox change color of certain words
本文介绍了C# - RichTextBox 更改某些单词的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能的重复:
如何从 RichTextBox 中选择文本然后着色
我真的没有任何代码可以显示,因为我不知道:(.我有一个输出带有标签信息的服务器.例如:
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
如何查看 Richtextbox 并将所有 ERROR 标签设为红色、INFO 标签设为绿色等?
How do I look in the richtextbox and make all ERROR tags red, INFO tags GREEN, etc.?
推荐答案
我认为这应该可以满足您的需求:
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;
}
将 StartsWith
更改为 Contains
这篇关于C# - RichTextBox 更改某些单词的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文