Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

In my RichtextBox, if I have written as below.

This is my pen,
his pen is beautiful.

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

All "is" should be highlighted.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
194 views
Welcome To Ask or Share your Answers For Others

1 Answer

What about:

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;
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...