开发者

changing the colour of certain characters and count in RichTextBox

开发者 https://www.devze.com 2023-04-10 05:20 出处:网络
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.

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 appreci开发者_JS百科ated.

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

my thanks in advance.


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:

changing the colour of certain characters and count in RichTextBox


Try this.

richTextBox1.ForeColor = Color.Red;

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号