How to highlight words in a richtextbox in vb.net

Goal: Change the font color of certain words in a richtextbox. This code example will search for the words "test" and "interloper" and make the font color green.

Instructions: Open a new Windows Forms Application and add 1 button to the form and a richtextbox. Add some text to the richtextbox. Replace all the automatically generated code in your project with the code below. Tip: also try RichTextBox1.BackColor = Color.Green. Not the fontcolor, but the background will be highlighted.

Code:

Public Class Form1
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
 
        'Start the highlight function. First parameter is the color. The other parameters are the words to highlight. Specify as many as you want.
        Highlight("green", "test", "interloper")
 
    End Sub
 
 
    'FUNCTION: Highlight text email
    Public Function Highlight(ByVal highlightcolor As String, ByVal ParamArray WordsToFind() As Object)
 
        'Loop through all the words you specified:
        For i As Integer = 0 To UBound(WordsToFind)
            RichTextBox1.Find(WordsToFind(i), RichTextBoxFinds.None)
 
            'Determine which color the word must become:
            Select Case highlightcolor
                Case "red"
                    RichTextBox1.SelectionColor = Color.Red
                Case "blue"
                    RichTextBox1.SelectionColor = Color.Blue
                Case "green"
                    RichTextBox1.SelectionColor = Color.Green
            End Select
 
            'reset the selection
            RichTextBox1.SelectionLength = 0
 
        Next i
 
        Return 1
 
    End Function
 
End Class