Richtextbox中的selectioncolor问题 [英] A problem with selectioncolor in richtextbox

查看:473
本文介绍了Richtextbox中的selectioncolor问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对以下代码有疑问:

I have a problem with follow code:

private void richTextBox1_TextChanged(object sender, EventArgs e)
{
    richTextBox1.SelectionColor = Color.Red;
}
private void richTextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == '\t')
    {
        e.Handled = true;
        richTextBox1.Text += "hell0;
    }
}


我只希望在按Tab键时,richtextbox添加"hell0".richtextbox中的文本不会更改颜色.但它变黑了.我不知道为什么.
请帮我!非常感谢.


I only want when I press tab key, richtextbox add "hell0".and text in richtextbox not change color. but it change black. I don''t know why.
please help me! thank so much.

推荐答案

确保将属性TabStop设置为False


The KeyPress事件在 发生.
首先,确保TabStop属性是false.
其次,+=运算符不适用于更改文本颜色.因此,您需要将代码更改为此:
The KeyPress event is raising before the TextChanged event.
First, make sure that the TabStop property is false.
Second, the += operator doesn''t work for changing text color. So, you need to change the code into this:
private void richTextBox1_TextChanged(object sender, EventArgs e)
{

}
private void richTextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == '\t')
    {
        richTextBox1.SelectionColor = Color.Red; // set selection color to red before appending text
        e.Handled = true;
        richTextBox1.AppendText("hell0"); // change richTextBox1.Text += "hell0" into richTextBox1.AppendText("hell0");
    }
}


希望对您有所帮助.


Hope this helps.


如果我已经理解了您的问题,则需要将"hello"的颜色更改为黑色.
您可以在文本中选择"hello",然后仅将所选内容更改为黑色.
If I''ve understood your question, you need to change the color of "hello" to black.
You can select "hello" in the text and then change color just for that piece of selected to black.


这篇关于Richtextbox中的selectioncolor问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆