C# How to Select and Color all text within a given line in Richtextbox?

Asked

Viewed 257 times

2

In C# I have a Richtextbox, inside it has several lines, what I want to do is select all text within a certain line and color. What I’m doing is this:

richTextBox1.Select(0, richTextBox1.Lines[index].Length);
richTextBox1.SelectionColor = Color.Red;    

Zero is the initial position of the text, index is the line number and Lenght is the one that will tell you the end of the selection within this line.

The problem is that it does not select the text of the current line, it always selects the text of the first line.

To be clearer, I need to do it in this style:

1
2
3 eu quero selecionar todo esse texto
4

How can I take the chain of a particular richTextBox line and within this chain select any text? and not "only" the first line?

1 answer

2


Use the method Getfirstcharindexfromline to obtain the position at which a given line starts.

To change the line color, do so:

richTextBox1.Select(richTextBox1.GetFirstCharIndexFromLine(index),
                    richTextBox1.Lines[index].Length);
richTextBox1.SelectionColor = Color.Red; 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.