突出显示要在 VB.NET 中查找的词

Highlight word to find in VB.NET

我有一个 RichTextBox(我需要查找所有单词的文本对应于 TextBox)、TextBox(用于键入要查找的单词)和一个 Button,当我单击 Button 时,我想在 RichTextBox 中,所有与 TextBox 中写入的单词对应的单词都用颜色突出显示(例如黄色)。我知道如何找到该词的第一次出现,但我不知道如何找到所有出现的地方。

只高亮第一次出现的单词的代码:

'CodeCS is my RichTextBox

CodeCS.SelectionBackColor = Color.White 
CodeCS.Find(ToolStripTextBox1.Text, RichTextBoxFinds.MatchCase)
CodeCS.SelectionBackColor = Color.Yellow

这里是搜索文本的简单循环 (rtb 是要搜索文本的 RichTextBox)

Sub HighlightWord(searchText As String)
    Dim len = searchText.Length
    Dim pos = rtb.Find(searchText, 0, RichTextBoxFinds.NoHighlight)
    While (pos >= 0)
        rtb.Select(pos, len)
        rtb.SelectionBackColor = Color.Yellow
        if pos + len  >= rtb.Text.Length Then
            Exit While
        End If
        pos = rtb.Find(searchText, pos + len, RichTextBoxFinds.NoHighlight)
    End While
End Sub