Reputation: 35
Given this text in a RichTextBox
:
str1
str2
str3
I don't know the length of all of them.
How can I paint the first line with green color and the third with blue?
I read a bit of the SelectionColor
but I don't want that the line will be marked.
There's another way? Or maybe that way (with SelectionColor
) but can you explain me how can I do it?
Upvotes: 1
Views: 580
Reputation: 39976
Try one of the following ways:
giving it's value like this:
richTextBox1.Find("str1");
richTextBox1.SelectionColor = Color.Green;
richTextBox1.Find("str3");
richTextBox1.SelectionColor = Color.Blue;
the other way like this:
richTextBox1.Select(0, 3);//Select text within 0 and 3
richTextBox1.SelectionColor = Color.Green;
richTextBox1.Select(9, 12);
richTextBox1.SelectionColor = Color.Blue;
Edit: To hide the selection add this line to end of your code :
richTextBox1.Select(0, 0);
Upvotes: 1
Reputation: 39152
I don't know the length of all of them.
Here's an alternate approach:
private void button1_Click(object sender, EventArgs e)
{
ColorLine(0, Color.Green);
ColorLine(2, Color.Blue);
}
private void ColorLine(int line, Color clr)
{
int index = richTextBox1.GetFirstCharIndexFromLine(line);
int length = richTextBox1.Lines[line].Length;
richTextBox1.Select(index, length);
richTextBox1.SelectionColor = clr;
richTextBox1.Select(0, 0);
}
Upvotes: 0