Coloring word "up" a limiter

Asked

Viewed 105 times

1

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

1- not paint because it has no limit

2- paint because it has limit: not paint

3- do not paint because there is no limit

4- paint because it has limit: not paint

(...) etc.

Note above that the limiter is :, the word will get colored how far this limiter is. After this limiter the text will look like it is.

I need to do this while the person types more specifically at the event Textchanged from Richtextbox and when FORM starts. If the person deleted the limiter, all the text that was in such a line will get one color only. Now if the person "placed" the back limiter, the word "before "this limiter will stay with color again.

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

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);
}
  • 1

    That! is more or less like this, only when you insert many lines in Rich with a limiter, only the first one turns red, has to serve for all lines "individually". You can do it like this?

  • Have, I’ll arrange, in case, each line has a delimiter right?

  • Ready to go, edited.

  • 1

    Exactly, each line has its delimiter. + 1

Browser other questions tagged

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