问题
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