find and select multiple words in richtextbox


Feb 28, 2008
Programming Experience
I have a richtextbox that I'm filling with text from a datagridview. Anywho, as the richtextbox is filling up I want to be able to highlight certain words that match my search word.

My complication at this point is changing the backcolor to all the items in the richtextbox that match the search word. I can change a single word with

txtSelection.SelectionStart = txtSelection.Find(txtSearch.Text)
txtSelection.SelectionBackColor = Color.Yellow

or simply:

txtSelection.SelectionBackColor = Color.Yellow

The search or selection both stop after finding and highlighting the first word. How would I extend, particularly the Find, to search through the entire richtextbox to select and highlight each matching word?
Use one of the other Find methods where you can specify at which index to start the search.
None of those find methods seem to offer any help for my specific situation. I need to be able to search the entire textbox and not stop at the first item. I can't see how I can use a loop to select all the matching items.
You have to use a loop. Find will only find once, if you give it a second thought, does the function return one or many results?? So you have to call Find many times. Each time you have to specify from where to start searching.
The find functionality clearly needs some tweaking because the way rtb.Find finds and selects text is done in a strange and illogical manner. There should obviously be a separate FindAll method applied to richtextboxes that finds and selects all instances of a search, which can be verified by the enormous amount of threads and tutorials of using Find in richtextboxes (all of which are overcomplicated).

The solution to this problem is laughable, especially when I read for about 4 hours and could not find the simple answer I knew existed. I saw people write multiple functions and sub procedures to do this:

Dim textEnd As Integer = txtSelection.TextLength
                Dim index As Integer = 0
                Dim lastIndex As Integer = txtSelection.Text.LastIndexOf(txtSearch.Text)

                While index < lastIndex
                    txtSelection.Find(txtSearch.Text, index, textEnd, RichTextBoxFinds.None)
                    txtSelection.SelectionBackColor = Color.Yellow
                    index = txtSelection.Text.IndexOf(txtSearch.Text, index) + 1
                End While

So there it is for the sake of others. I couldn't find one Google hit where this code appeared. :rolleyes: I thank God for wisdom that Google and searching multiple forums could not impart.
ActiveReports Variation

Thanks so much for posting this code. I was able to use it with the slight modifications below in an ActiveReports 7.0 report I am developing. I call this function from the details section format event on the report. The sContents variable contains the text in the richtextbox control.

Public Sub SetRichTextProperties(ByVal rtb As GrapeCity.ActiveReports.SectionReportModel.RichTextBox, Optional ByVal sContents As String = "")

If sContents <> "" Then
Dim sTextToFind As String = "DISMISSED"
Dim textEnd As Integer = rtb.Text.Length
Dim index As Integer = 0
Dim lastIndex As Integer = rtb.Text.LastIndexOf(sTextToFind)
While index < lastIndex
index = rtb.Find(sTextToFind, index, SectionReportModel.RichTextBox.FindOptions.None)
rtb.SelectionStart = index
rtb.SelectionLength = 9 'matches length of sTextToFind, would need modifications to improve for any desired text
rtb.SelectionFont = New Font("Times New Roman", 8, FontStyle.Bold)
index = rtb.Text.IndexOf(sTextToFind, index) + 1
End While
Catch ex As Exception
Exit Try
End Try
End If
End Sub

Latest posts