突出显示要在VB.NET中查找的单词 [英] Highlight word to find in VB.NET
问题描述
我有一个RichTextBox(需要查找所有单词的文本对应于TextBox),TextBox(用于键入要查找的单词)和一个Button,当我单击Button时,我希望在在RichTextBox中,所有与TextBox中写入的单词相对应的单词都用一种颜色突出显示(例如,黄色).我知道如何找到单词的第一个出现的位置,但我不知道如何找到所有出现的位置的单词.
I have a RichTextBox (the text where I need to find all the word corresponds to the TextBox), TextBox (for typing the word to find) and a Button, and when I click on the Button, I would like that in the RichTextBox, all the words corresponding to the word written in the TextBox are highlighted with a color (yellow for example). I know how to find the first occurrence of the word but I do not know how to find all the occurrences.
仅突出显示单词首次出现的代码:
The code for highlighting only the first occurrence of the word:
'CodeCS is my RichTextBox
CodeCS.SelectionBackColor = Color.White
CodeCS.Find(ToolStripTextBox1.Text, RichTextBoxFinds.MatchCase)
CodeCS.SelectionBackColor = Color.Yellow
推荐答案
以下是搜索文本的简单循环 (rtb是用于搜索文本的RichTextBox)
Here a simple loop over the searched text (rtb is the RichTextBox to search the text for)
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
这篇关于突出显示要在VB.NET中查找的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!