Color word "up" a limiter

1

I need to color in the RichTextBox filled with several lines, a text before a limiter, Note for example the highlights:

1- not to paint because there is no limit

2 Paint because it has a limit : do not paint

3- do not paint because there is no limit

4 Paint because it has a limit : do not paint

(...) etc

Notice above that the limiter is : , the word will get color how far this limiter is. After this limiter the text will stay as it is.

I need to do this as the person types more specifically in the TextChanged event of the RichTextBox and when the FORM starts. If the person deleted the limiter, all text that was on such a line will have a single color . Now if the person "" put the back limiter, the word " before " of that limiter will get color again.

I found that the REGEX class is a good helper for this, however, I have no idea how to use it wisely.

    
asked by anonymous 10.01.2018 / 14:32

1 answer

1

If I understand what you want, here it is:

private void richTextBox1_TextChanged(object sender, EventArgs e)
{
    int current = richTextBox1.SelectionStart;
    for(int i = 0; i < richTextBox1.Lines.Length; i++)
    {
        string line = richTextBox1.Lines[i];

        int index = line.IndexOf(':'), lineFirstIndex = richTextBox1.GetFirstCharIndexFromLine(i);
        if (index != -1)
        {
            richTextBox1.Select(lineFirstIndex, index);
            richTextBox1.SelectionColor = Color.Red;
        }
        else
        {
            richTextBox1.Select(lineFirstIndex, line.Length);
            richTextBox1.SelectionColor = Color.Empty;
        }
    }
    richTextBox1.Select(current, 0);
}
    
10.01.2018 / 19:02