如何使用不同颜色为RichTextBox中的不同单词着色?

如何以不同方式为RichTextBox的部分着色?

string text = "abcd 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 = Color.Blue .AppendText(“Blue text”).SelectionColor = Color.Black .AppendText(“black text”)End With