簡體   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