The Mask
The Mask

Reputation: 17427

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

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.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?
}

Upvotes: 2

Views: 6292

Answers (2)

Jan
Jan

Reputation: 1

Visual Basic code: With RichTextBox1 .SelectionColor = Color.Blue .AppendText("Blue text") .SelectionColor = Color.Black .AppendText(" black text") End With

Upvotes: 0

Waleed
Waleed

Reputation: 3145

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);

Upvotes: 3

Related Questions