Change color of expressions that begin with '@'

-1

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

For example:

nothing @examples nadamais

The @expressao must be of another color.

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

I already tried to separate the string from the textbox with split and detect which of them starts with @. But after that, I could not color the letters and send them back to the textbox.

I also tried something with the selectionColor of the richTextBox, however the selection would clear the @ and the cursor would go to the left of the inserted character, besides 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 that is in the comments to try to make it work in my case, but the limiter does not work. I believe this happens because the selection starts only from an '@', 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);
    }
    
asked by anonymous 26.01.2018 / 13:18

1 answer

0

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

"@ expression xxxxxx" , only the @expression has the background changed. By the time I delete the X and space, then everything I write from there has the background color changed too.

I do not know if this is some property of SelectionBackColor, because if I change to SelectionColor, changing the font color, that problem does not happen.

Follow 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 = '
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;
        }
    }
'; 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 color:

 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 = '
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;
        }
    }
'; 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++; } } } } }
    
01.02.2018 / 18:49