在RichTextBox中选择文本着色 [英] Selectively coloring text in RichTextBox

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

问题描述

我怎样才能在红色每当我满足RichTextBox中的字母A的时间画画吗?

How can I paint in red every time I meet the letter "A" in RichTextBox?

推荐答案

试试这个:

static void HighlightPhrase(RichTextBox box, string phrase, Color color) {
  int pos = box.SelectionStart;
  string s = box.Text;
  for (int ix = 0; ; ) {
    int jx = s.IndexOf(phrase, ix, StringComparison.CurrentCultureIgnoreCase);
    if (jx < 0) break;
    box.SelectionStart = jx;
    box.SelectionLength = phrase.Length;
    box.SelectionColor = color;
    ix = jx + 1;
  }
  box.SelectionStart = pos;
  box.SelectionLength = 0;
}

...

private void button1_Click(object sender, EventArgs e) {
  richTextBox1.Text = "Aardvarks are strange animals";
  HighlightPhrase(richTextBox1, "a", Color.Red);
}

这篇关于在RichTextBox中选择文本着色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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