Search RichTextBox Highlight each word HOW?

vbMarkO

Centurion
Joined
Aug 19, 2005
Messages
157
I have searched all over for this one....

I have foundout how to highlight one word but each instance of a word...


Visual Basic:
rtb1.Find("Line", RichTextBoxFinds.MatchCase)
rtb1.SelectionBackColor = Color.Yellow

This will find the first instance of the word and Highlight it but how can I get
every instance of the word within the rtb1 text and highlight them?

I have found 3rd party controls but surely this can be done without them!

vbMarkO
 
It might not be the best way, but something like this should work.
Visual Basic:
        Dim int As Integer = 0
        Dim search As String = "this"
        While int <> -1
            int = RichTextBox1.Text.IndexOf(search, int)
            If int <> -1 Then
                RichTextBox1.SelectionStart = int
                RichTextBox1.SelectionLength = search.Length
                RichTextBox1.SelectionColor = Color.Yellow
                int = int + search.Length
            End If
        End While
[edit]NB. I used SelectionColor not the background one you used as that doesn't appear to be available in .Net 1.1[/edit]
 
Back
Top