Highlight all searched words

后端 未结 6 753
耶瑟儿~
耶瑟儿~ 2020-12-16 00:35

In my RichtextBox, if I have written as below.

This is my pen,
his pen is beautiful.

Now I search word \"is\"

6条回答
  •  情深已故
    2020-12-16 01:17

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

提交回复
热议问题