简体   繁体   English

RichTextBox更改颜色的文本

[英]RichTextBox change text of color

I have RichTextBox control with some BASIC TEXT entered. 我有输入一些基本文本的RichTextBox控件。 I wish to customize it in a way that, when user will enter or paste some text this would have different font color than the BASIC TEXT. 我希望以一种方式自定义它,当用户输入或粘贴一些文本时,其字体颜色将与“基本文本”不同。

i was changing SelectionBackColor prperty on KeyDown event, but it occurs also when user tries only to copy part of BASIC TEXT. 我在KeyDown事件上更改了SelectionBackColor属性,但是当用户尝试仅复制部分BASIC TEXT时也会发生。

 private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
    {
        RichTextBox richTextBox1 = sender as RichTextBox;
        richTextBox1.SelectionBackColor = Color.LightCoral;
    }

Instead of KeyDown , use TextChanged event: 代替KeyDown ,使用TextChanged事件:

private void richTextBox1_TextChanged(object sender, EventArgs e)
{
    RichTextBox richTextBox1 = sender as RichTextBox;
    richTextBox1.SelectionBackColor = Color.LightCoral;
}

Try this : 尝试这个 :

private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Control && e.KeyCode == Keys.V)
        {
            richTextBox1.SelectionBackColor = Color.LightCoral;

        }
        else
        {
            richTextBox1.SelectionBackColor = Color.White;
        }
    }
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Control && e.KeyCode == Keys.V)
    {
        richTextBox1.SelectionBackColor = Color.LightCoral;

    }
    else
    {
        richTextBox1.SelectionBackColor = Color.White;
    }
}

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

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