Resolve color problem in richtextbox in C #

0

How can I make a click on button1 the text of richTextBox2 become a certain color, and I click on button2 text from the vertical bar | become the color of button2 , my code below effects the color change of the entire text. I would appreciate your help, thanks in advance!

Code:

   {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            richTextBox2.ForeColor = Color.Green;

        }

        private void button2_Click(object sender, EventArgs e)
        {
            richTextBox2.ForeColor = Color.Red;

        }


        private void richTextBox2_TextChanged(object sender, EventArgs e)
        {
            richTextBox2.Text = richTextBox1.Text;
        }
    }
}
    
asked by anonymous 30.01.2016 / 20:20

1 answer

0

If it's something simple like just searching for a character and modifying the color from it, you can do something like:

int inicio = richTextBox2.Text.IndexOf('|');
if(inicio >= 0)
{
    richTextBox2.SelectionStart = inicio;
    richTextBox2.SelectionLength = richTextBox2.Text.Length - inicio;
    richTextBox2.SelectionColor = Color.Red;
}
    
30.01.2016 / 22:28