突出显示所有搜索到的单词 [英] Highlight all searched words

查看:69
本文介绍了突出显示所有搜索到的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 RichtextBox 中,如果我写的如下。

In my RichtextBox, if I have written as below.


此是我的笔,

他的笔很漂亮。

This is my pen,
his pen is beautiful.

现在我搜索单词 is然后是
的输出将如下所示。

Now I search word "is" then output would be as below.

所有是都应突出显示。

推荐答案

什么:

static class Utility {
    public static void HighlightText(this RichTextBox myRtb, string word, Color color) {  

       if (word == string.Empty)
            return;

       int s_start = myRtb.SelectionStart, startIndex = 0, index;

       while((index = myRtb.Text.IndexOf(word, startIndex)) != -1) {
           myRtb.Select(index, word.Length);
           myRtb.SelectionColor = color;

           startIndex = index + word.Length;
       }

       myRtb.SelectionStart = s_start;
       myRtb.SelectionLength = 0;
       myRtb.SelectionColor = Color.Black;
    }
}

这篇关于突出显示所有搜索到的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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