简体   繁体   English

如何使用C#在RichTextBox中设置文本的颜色?

[英]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. 我在应用程序中有一个Richtextbox,我想使用几种颜色在文本框中显示文本。

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 : 选择文本,然后设置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;
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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