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

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

问题描述

在我的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.

所有是"都应突出显示.

All "is" should be highlighted.

推荐答案

怎么样:

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天全站免登陆