如何更改richtextbox中2个字符之间的颜色?

How to change the color between 2 characters in richtextbox?

如何在 richtextbox 中改变 2 个字符之间的颜色?

我写了这样的代码,但这次它没有得到最后一个字符。

        if (e.KeyChar=='"')
        {
            if (richTextBox1.SelectionColor==Color.Black)
            {
                richTextBox1.SelectionColor = Color.Red;
            }
            else if (richTextBox1.SelectionColor == Color.Red)
            {
                richTextBox1.SelectionColor = Color.Black;
            }
        }

输出:"Hello World" 我得到这样的结果。

谢谢

你的问题不清楚,但我试着理解你正试图在 richtextbox 中给颜色值 2 x 2 。

并且您在 richTextBox1_KeyDown

中编写了代码

尝试在下面编写代码并更改颜色以查看会发生什么。

private void richTextBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        char ss = e.KeyChar; //you can do what you want with this value in your case then 
        if (richTextBox1.TextLength > 0)
        {
            if (richTextBox1.TextLength % 2 == 0)
            {
                if (richTextBox1.SelectionColor == Color.Black)
                {
                    richTextBox1.SelectionColor = Color.Red;
                }
                else if (richTextBox1.SelectionColor == Color.Red)
                {
                    richTextBox1.SelectionColor = Color.Black;
                }
            }

        }
    }