How to color sections of a RichTextBox differently?
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.BlueVio开发者_JAVA技巧let, 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?
}
you should use RichTextBox to color your lines of text, use this snippet.
txtRichTextBox.Select(yourText.IndexOf("portion"), "portion".Length);
txtRichTextBox.SelectionColor = YourColor;
txtRichTextBox.SelectionFont = new Font("Times New Roman",FontStyle.Bold);
Visual Basic code: With RichTextBox1 .SelectionColor = Color.Blue .AppendText("Blue text") .SelectionColor = Color.Black .AppendText(" black text") End With
精彩评论