0
votes

I have a richtextbox in an app and I'd like to show text in the textbox using several colors.

How can I do this?

For example, I want to show the first line in red color, the second line in green color and the third line in black color.

2
for example i want to show the first color in red color the second line in green color in the third color in black color> - qismatullah sultani

2 Answers

0
votes

Select the text and then set the SelectionColor:

// Makes the first 3 characters red.
richTextBox1.Select(0,3);
richTextBox1.SelectionColor = Color.Red;
0
votes

You have to look at this

List<Color> C;
Int32 counter = 0;

private void Form1_Load(object sender, EventArgs e)
        {
            C = new List<Color>();
            C.Add(Color.AliceBlue);
            C.Add(Color.AntiqueWhite);
            C.Add(Color.Aqua);
            C.Add(Color.Aquamarine);
            C.Add(Color.Azure);
            C.Add(Color.Beige);
            C.Add(Color.Black);
            C.Add(Color.BlanchedAlmond);
            C.Add(Color.Blue);
            C.Add(Color.BlueViolet);
        }

private void richTextBox1_TextChanged(object sender, EventArgs e)
        {
            //richTextBox1.SelectionStart = 1;
            //richTextBox1.SelectionLength = mystring.Length;
            richTextBox1.SelectionColor = C[counter];
            counter++;
            if (counter >= 10)
            {
                counter = 0;
            }
        }