Change expression color starting with@

Asked

Viewed 237 times

-1

I need to create a winform to change the color of expressions that start with '@'.

For example:

nothing @expresso nada

The @expression needs to be another color.

The text will come from the input of a textbox that the user type (can be a richTextBox or something from Infragistics, whatever, as long as it works).

I already tried to separate the string from the split textbox and detect which ones start with @. But after that, I couldn’t color the letters and send them back to the textbox.

I also tried something with the richTextBox selectionColor, but the selection erased the @ and the cursor went to the left of the inserted character, and I could not "stop" the selection when I wanted.

I’m taking a look at Regex to see if I can apply to my problem.

I’m using C# in Visual Studio Community 2017.

EDIT

I changed the code in the comments to try to make it work in my case, but the limiter doesn’t work. I believe this is because the selection starts only from a@, but there may be a ' before, hence Buga.

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 = line.IndexOf('@');

            if (index != -1 && lineFirstIndex != -1)
            {
                richTextBox1.Select(lineFirstIndex, index);
                richTextBox1.SelectionColor = Color.Red;
            }
            else
            {
                lineFirstIndex = richTextBox1.GetFirstCharIndexFromLine(i);

                richTextBox1.Select(lineFirstIndex, line.Length);
                richTextBox1.SelectionColor = Color.Empty;

            }
        }
        richTextBox1.Select(current, 0);
    }
  • Out of curiosity: you asked this in Soen too, right?

  • I asked you kkk

  • Ah, good. I had her open to leave a comment, but here it’s even better. Anyway, you need to give us a little more detail and show us what you’ve tried.

  • I’ll edit the post then. It’s the first time I’m posting here kk

  • Would that be about right? https://answall.com/questions/268162/colorir-palavra-at%C3%A9-a-limiter

  • I put the code on Edit

Show 1 more comment

1 answer

0

I was able to change the background of the expressions. However, if I have a text like

"@Expression xxxxxx", only @Expression has the background changed. By the time I erase the X’s and the space, then everything I write from there has the background color changed too.

I don’t know if this is some property of Selectionbackcolor, because if I switch to Selectioncolor, changing the font color, this problem does not happen.

Follows the code:

 private void FindAt() //acha arrobas
    {
        for(int r = 0; r <= tam; r++)
        {
            for(int z = 0; z < (tam - r); z++)
            {
                if (z > 8) break;

                String temp = text.Substring(r, z);
                char aux = '\0';

                if (lista.FindStringExact(temp) != -1)
                {
                    if (tam > r + z) aux = text.ElementAt(r + z);
                    if ((r > 0 && text.ElementAt(r - 1) == '@') && aux == ' ')
                    {
                        atIndex[0, ctrlAt] = r-1;   //salva posição inicial da variavel, incluindo o @ (por isso o -1)
                        atIndex[1, ctrlAt] = z + 1;   //salva comprimento da variavel, + 1 por causa do @ (segundo argumento da substring)
                        ctrlAt++;
                    }
                }
            }
        }
    }

And the code that changes the color:

private void ColorAt() //colore variaveis
    {
        for (int a = 0; a < ctrlAt; a++)
        {
            if (atIndex[0, a] != -1)
            {
                richTextBox1.Select(atIndex[0, a], atIndex[1, a]);
                richTextBox1.SelectionBackColor = Color.Green;
            }
            else richTextBox1.SelectionBackColor = Color.Empty;
        }
    }

Browser other questions tagged

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