Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Highlight all searched words

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.

like image 832
Khilen Maniyar Avatar asked Aug 07 '12 18:08

Khilen Maniyar


People also ask

How do I highlight multiple search strings in word?

Enter a search string in the search control. I entered very (Figure B). Click Reading Highlight and choose Highlight All and Word will highlight all instances of very.

How do you highlight all words at once?

If you want to highlight a whole line of text, move your cursor to the start of the line, hold the Shift key, and then press the Down arrow . You may also use the shortcut key combination Shift + End . If you want to highlight all text (the entire page), press the shortcut key Ctrl + A .


1 Answers

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;
    }
}
like image 147
Omar Avatar answered Oct 14 '22 04:10

Omar