Vb.net 如何在visual Basic中为一个单词的多个实例着色

Vb.net 如何在visual Basic中为一个单词的多个实例着色,vb.net,Vb.net,当我使用 RichTextBox1.SelectionStart = RichTextBox1.Find("println.") RichTextBox1.SelectionColor = Color.Red 它只为println中的一个单词着色。所以我想知道如何给println这个词的多个实例着色。这是一个想法 Do Until 1 = 2 RichTextBox1.SelectionStart = RichTextBox1.Find("println.") RichText

当我使用

RichTextBox1.SelectionStart = RichTextBox1.Find("println.")
RichTextBox1.SelectionColor = Color.Red
它只为println中的一个单词着色。所以我想知道如何给println这个词的多个实例着色。这是一个想法

Do Until 1 = 2
    RichTextBox1.SelectionStart = RichTextBox1.Find("println.")
    RichTextBox1.SelectionColor = Color.Red
Loop

但是我不确定这是否会起作用。你可以使用一个循环,只要找到单词的实例,它就会一直运行。在每个循环的末尾,将IndexOf的起点设置为你刚才突出显示的单词的末尾。这样,循环基本上通过字符串移动,并忽略它在以前的迭代中已经搜索过的区域。以下是该概念的一个示例:

    rtb1.Text = "one two three one two three one two three" 'text to search
    Dim search As String = "three" 'our search word

    Dim i As Integer = 0
    'while there is still another instance of our search word
    While i <> -1
        'get the first index starting after our previous index value
        i = rtb1.Text.IndexOf(search, i)
        If i <> -1 Then 'if we have one
            'then get the index of the end of the word so we can select it
            Dim iEnd As Integer = rtb1.Text.IndexOf(search, i) + search.Length
            rtb1.Select(i, search.Length) 'select the word
            rtb1.SelectionFont = New Font("Arial", 12, FontStyle.Bold) 'set its font
            'then, set our start point for the next loop iteration equal to the end of the word we just highlighted
            i = iEnd
        End If
    End While
rtb1.Text=“一二三一二三一二三”要搜索的文本
Dim search As String=“three”'我们的搜索词
尺寸i为整数=0
“而我们的搜索词还有另一个实例
而i-1
'获取在上一个索引值之后开始的第一个索引
i=rtb1.Text.IndexOf(搜索,i)
如果i-1,那么“如果我们有一个
'然后获取单词末尾的索引,以便我们可以选择它
Dim iEnd As Integer=rtb1.Text.IndexOf(search,i)+search.Length
rtb1.Select(i,search.Length)'选择单词
rtb1.SelectionFont=新字体(“Arial”,12,FontStyle.Bold)”设置其字体
'然后,将下一个循环迭代的起点设置为刚才突出显示的单词的结尾
i=iEnd
如果结束
结束时
上述内容将rtb的文本更改为:

一二三一二三一二三

致:

一二一二一二


要稍微扩展Nico的评论,有一个重载,允许您指定起始位置,因此您需要跟踪当前位置,记住跳过找到的文本,这样您就不会重复找到相同的事件:

Dim start = 0
Do
    start = RichTextBox1.Find(searchText, start, RichTextBoxFinds.None)
    If start >= 0 Then
        RichTextBox1.SelectionColor = Color.Red
        start += searchText.Length ' skip current occurrence
    End If
Loop While start >= 0 AndAlso start < RichTextBox1.Text.Length
Dim start=0
做
start=RichTextBox1.Find(searchText,start,RichTextBoxFinds.None)
如果start>=0,则
RichTextBox1.SelectionColor=Color.Red
start+=searchText.Length'跳过当前事件
如果结束
开始时循环>=0,开始时循环
注意:如评论中所述进行了更新,尽管我不完全理解为什么当您传入一个等于文本长度的开始时RTB似乎会重新启动搜索

更新:查看for
RichTextBox.Find(String,Int32,RichTextBoxFinds)
显示,如果传入的
start
等于字符串长度且
end
等于-1(此重载的默认值),则搜索范围将重置为包含整个文本,从而重新启动搜索并产生@RawN发现的无限循环。我不明白为什么它是这样工作的,但它解释了需要额外的
和开始
检查。

谢谢您的帮助

  Dim search As String = "println." 'our search word
    Dim i As Integer = 0
    'while there is still another instance of our search word
    'println.
    While i <> -1
        'get the first index starting after our previous index value
        i = RichTextBox1.Text.IndexOf(search, i)
        If i <> -1 Then 'if we have one
            'then get the index of the end of the word so we can select it
            Dim iEnd As Integer = RichTextBox1.Text.IndexOf(search, i) + search.Length
            RichTextBox1.Select(i, search.Length) 'select the word
            RichTextBox1.SelectionColor = Color.Red 'set its font
            'then, set our start point for the next loop iteration equal to the end of the word we just highlighted
            i = iEnd
        End If
    End While
Dim search As String=“println.”我们的搜索词
尺寸i为整数=0
“而我们的搜索词还有另一个实例
“普林顿。
而i-1
'获取在上一个索引值之后开始的第一个索引
i=RichTextBox1.Text.IndexOf(搜索,i)
如果i-1,那么“如果我们有一个
'然后获取单词末尾的索引,以便我们可以选择它
Dim iEnd As Integer=RichTextBox1.Text.IndexOf(search,i)+search.Length
RichTextBox1.Select(i,search.Length)'选择单词
RichTextBox1.SelectionColor=Color.Red'设置其字体
'然后,将下一个循环迭代的起点设置为刚才突出显示的单词的结尾
i=iEnd
如果结束
结束时

这就是我所使用的,它对我所做的工作非常有用,它对PrimTLN字的所有实例都进行了着色。

使用“代码> >查找())/<代码>的重载,这是一个开始索引。请再试一次。提示:
start=0。。。在开始时循环>=0
@RawN和
start=RichTextBox1.Find(searchText,start,RichTextBoxFinds.None)
将在未找到时将其设置为-1,或在找到时将其设置为索引(0+)。我仍然不明白你的逻辑。如果RichEdit中的最后一个单词等于searchText,那么你的代码将以无限循环结束。一定要试试看。@RawN谢谢,这就解释了为什么我没有看到这个循环。有趣的是,指定一个等于文本长度的开始索引似乎可以重新开始搜索,即使开始索引超出范围。指定文本长度+1的开头会导致异常,这使得异常更加奇怪。我将修改代码以检查这种情况,但我还需要阅读RTB的源代码,以理解为什么它会这样做,因为文档似乎没有涵盖它。