开发者

How to color different words in a RichTextBox with different colors?

开发者 https://www.devze.com 2023-03-12 21:07 出处:网络
How to color sections of a RichTextBox differently? string text = \"a b c d teste\"; // words to highlight

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

0

精彩评论

暂无评论...
验证码 换一张
取 消