问题描述
|
如何为RichTextBox的各个部分上色?
string text = \"a b c d teste\";
// words to highlight
string[] word = { \"a\",\"b\",\"c\",\"d\" };
// colors to use,aligned with words above
Color[] color = { Color.Red,Color.Blue,Color.BlueViolet,Color.Brown };
for(int c = 0,size = word.Length; c < size; c++) {
//search by color[x] and set line color to color[x]
//How I do this?
}
解决方法
您应该使用RichTextBox为文本行着色,使用此代码段。
txtRichTextBox.Select(yourText.IndexOf(\"portion\"),\"portion\".Length);
txtRichTextBox.SelectionColor = YourColor;
txtRichTextBox.SelectionFont = new Font(\"Times New Roman\",FontStyle.Bold);
, Visual Basic代码:
使用RichTextBox1
.SelectionColor =颜色。蓝色
.AppendText(\“蓝色文字\”)
.SelectionColor =颜色。黑色
.AppendText(\“黑色文字\”)
结束于