简体   繁体   中英

How can I set the color of text in a richtextbox using c#?

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.

Select the text and then set the SelectionColor :

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

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;
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM