在 RichTextBox 中更改某些字符的颜色和计数 [英] changing the colour of certain characters and count in RichTextBox

查看:76
本文介绍了在 RichTextBox 中更改某些字符的颜色和计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我必须计算 C# 富文本框中某个字符的数量(即:K").
但我也想在按下按钮时在富文本框中为该字符指定颜色.简而言之,我陷入困境,任何帮助表示赞赏.

p.s:我试过谷歌搜索,但找不到令人满意的解决方案.
提前致谢.


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.

推荐答案

这是代码示例:

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

此示例为富文本框中的 K 个字母着色.它并不理想,但您将了解如何为文本着色.

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

结果如下:

这篇关于在 RichTextBox 中更改某些字符的颜色和计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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