I recently started to learn rich text format and i am encountering a bizarre behavior.
Here is the code:

    private void button2_Click_1(object sender, EventArgs e)
        {
            s = richTextBox1.Rtf; 
            s = s.Insert(s.IndexOf("colortbl ;") + 10, @"\red0\green255\blue0;\red128\green0\blue0;\red128\green128\blue0;");

            LockWindowUpdate(richTextBox1.Handle);

            string t = @"{\cf3  test }";
            s = s.Insert(s.IndexOf("int") + 3, t); //insert it, for example, after the word "int"

            richTextBox1.Rtf = s;
            richTextBox1.SelectionStart = richTextBox1.Text.Length;

            LockWindowUpdate(IntPtr.Zero);
        } 

The rtf of the richtextbox before editing is:

{\rtf1\ansi\deff0{\fonttbl{\f0\fnil\fcharset0 Consolas;}}
{\colortbl ;\red0\green255\blue0;}
\viewkind4\uc1\pard\cf1\lang1033\f0\fs20 This line is an int\line This line is an int  \line This line is an int \par
}

Every time I insert the string t (by pressing a button) , it is colored, alternatively, in the second and the third color from the table instead only in the third color ( t is automatically modified in "{\cf2 test}" and then back in "{\cf3 test }").

Here is an example after two button clicks:

{\rtf1\ansi\ansicpg1252\deff0{\fonttbl{\f0\fnil\fcharset0 Consolas;}}
{\colortbl ;\red0\green255\blue0;\red128\green128\blue0;\red128\green0\blue0;}
\viewkind4\uc1\pard\cf1\lang1033\f0\fs20 This line is an int **\cf2  test \cf3  test**  \cf1\line This line is an int\line This line is an int \par
}

I will provide any extra information needed.
This in my first post and I hope I haven't done anything wrong. :D

Maybe inserting values directly is a tricky way.
Please, read this article. I am sure it will help you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.