简体   繁体   中英

RichTextBox change text of color

I have RichTextBox control with some BASIC TEXT entered. 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.

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

Instead of KeyDown , use TextChanged event:

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

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