0
I made a code to change the backcolor of all richTextBox expressions starting with a @. It’s all ok, however, there is an error that I’m not able to solve. If the first thing I write is @expression and then I delete everything, when I start writing something again, it starts with the backcolor changed, and it should be normal, without it, unless it was something starting with @.
I’m trying to see line by line where the problem might be, but I can’t find.
Follows the code:
//variável para controlar o índice dos vetores
    int ctrlAt = 0;
    //flags de controle
    Boolean flagAt = false;
    //tamanho do texto da richTextBox
    int tam = 0;
    //variável para controle dos espaços
    int last = 5000;
    //index dos espaços
    int[] spaceIndex = new int[1000];
    //index dos @
    int[] atIndex = new int[1000];
    //reseta vetor dos @
    public void atVetor()
    {
        for (int r = 0; r < 1000; r++)
        {
            atIndex[r] = -1;
        }
    }
    //reseta vetor dos espaços
    public void spaceVetor()
    {      
        for (int r = 0; r < 1000; r++)
        {
            spaceIndex[r] = -1;
        }
    }
private void richTextBox1_TextChanged(object sender, EventArgs e)
    {
        //variável com o índice atual do cursor
        int current = richTextBox1.SelectionStart;
        //string com o conteúdo do texto da richTextBox
        string text = richTextBox1.Text;
        if (text.Length < tam)
         {
            //se algum caractere foi apagado, reseta vetores e variáveis de controle
            tam = text.Length;
            atVetor();
            spaceVetor();
            ctrlAt = 0;
            richTextBox1.Select(current, 0);
            richTextBox1.SelectionColor = Color.Empty;
            richTextBox1.SelectionBackColor = Color.Empty;
        }
        else tam = text.Length;
        //percorre o texto
        for (int q = 0; q < tam; q++)
        {
            //se encontrar um @, salva seu índice no vetor
            if (text.ElementAt(q) == '@')
            {
                if (flagAt == true && q > atIndex[ctrlAt])
                {
                    //controle de dois @ sem espaço entre eles
                    if (spaceIndex[ctrlAt] != -1)
                    {
                        ctrlAt++;
                        atIndex[ctrlAt] = q;
                    }
                }
                //if específico para o primeiro @
                if (ctrlAt == 0 && atIndex[ctrlAt] == -1)
                {
                    atIndex[ctrlAt] = q;
                    flagAt = true;
                }
            }
        }
        if (atIndex[ctrlAt] != -1)
        {
            //percorre o texto
            for (int b = atIndex[ctrlAt] + 1; b < text.Length; b++)
            {
                //se achar um espaço que não seja o último encontrado, salva o seu índice no vetor
                if (text.ElementAt(b) == ' ' && spaceIndex[ctrlAt] != last)
                {
                    spaceIndex[ctrlAt] = b;
                    last = spaceIndex[ctrlAt];
                    break;
                }
            }
            //repete vezes equivalentes ao número de @'s no texto
            for (int a = 0; a <= ctrlAt; a++)
            {
                if (spaceIndex[a] != -1)
                {
                    //altera a cor do intervalo entre o @ e o espaço
                    richTextBox1.Select(atIndex[a], spaceIndex[a] - atIndex[a]);
                    richTextBox1.SelectionBackColor = Color.LightBlue;
                    richTextBox1.SelectionColor = Color.Blue;
                    //deixa tudo depois disso na cor original
                    richTextBox1.Select(current, 0);
                    richTextBox1.SelectionBackColor = Color.Empty;
                    richTextBox1.SelectionColor = Color.Empty;
                }
                else
                {
                    richTextBox1.Select(atIndex[ctrlAt], text.Length);
                    richTextBox1.SelectionBackColor = Color.Empty;
                    richTextBox1.SelectionColor = Color.Empty;
                }
            }
        }
        //faz o cursor ir para o fim do texto e não selecionar nada
        richTextBox1.Select(current, 0);
    }
I applied those amendments, but it did not resolve them. And I believe that the flag has nothing to do with it, because it controls the location of the @ in the array, and the colored background is getting into anything I type after deleting, with or without @’s.
– Eduardo Remor
I advise you to debug the code to understand why it is coming to the line of code: richTextBox1.Select(atIndex[a], spaceIndex[a] - atIndex[a]); Instead of going into Else, that cleans the background.
– Felipe Grossi