繁体   English   中英

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

[英]Highlight word to find in VB.NET

我有一个RichTextBox(我需要在其中查找所有单词的文本对应于TextBox),TextBox(用于键入要查找的单词)和一个Button,当我单击Button时,我希望在RichTextBox中找到它,所有与“文本框”中写入的单词相对应的单词均以颜色突出显示(例如,黄色)。 我知道如何找到单词的第一个出现,但我不知道如何找到所有出现的单词。

仅突出显示单词首次出现的代码:

'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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM