changing the colour of certain characters and count in RichTextBox

旧巷老猫 提交于 2019-12-08 03:20:37

问题



i have to count the number of a certain character in a C# richtextbox (namely: "K").
but i also want to give that character a colour in the rich text box when a button is pressed. in short, i'm in a bind, any help is appreciated.

p.s: i tried googleing it but couldn't find a statisfactory solution.
my thanks in advance.


回答1:


Here is code example:

private void ColorTheKs()
{
    for(int i = 0; i< richTextBox1.Text.Length; i++)
    {
        if (richTextBox1.Text[i] == 'K')
        {
            richTextBox1.SelectionStart = i;
            richTextBox1.SelectionLength = 1;
            richTextBox1.SelectionColor = Color.Red;
            richTextBox1.SelectionBackColor = Color.Yellow;
        }
    }
}

this example colors K letters in rich text box. Its not ideal but you'll get a point how you can color text.

Here is the result:




回答2:


Try this.

richTextBox1.ForeColor = Color.Red;



来源:https://stackoverflow.com/questions/7645010/changing-the-colour-of-certain-characters-and-count-in-richtextbox

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!