[英]Highlight word to find in VB.NET
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). 我有一个RichTextBox(我需要在其中查找所有单词的文本对应于TextBox),TextBox(用于键入要查找的单词)和一个Button,当我单击Button时,我希望在RichTextBox中找到它,所有与“文本框”中写入的单词相对应的单词均以颜色突出显示(例如,黄色)。 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
Here a simple loop over the searched text (rtb is the RichTextBox to search the text for) 这里是搜索文本的简单循环(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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.