C # How to Select and Color all text within a given row in RichTextbox?

2

In C # I have a RichTextbox, inside it has several lines, what I want to do is to select all text within a given 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, the index is the number of the line and Lenght is what will tell 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 get clearer, I need to do this:

1
2
3 eu quero selecionar todo esse texto
4

How can I get the current of a certain row of the richTextBox and within this chain select all text? and not "only" the first line?

    
asked by anonymous 06.01.2018 / 23:29

1 answer

2

Use the GetFirstCharIndexFromLine to get the position where a particular line starts.

To change the color of the line, do the following:

richTextBox1.Select(richTextBox1.GetFirstCharIndexFromLine(index),
                    richTextBox1.Lines[index].Length);
richTextBox1.SelectionColor = Color.Red; 
    
07.01.2018 / 00:05