Reese Houseknecht
Reese Houseknecht

Reputation: 131

How to color multiple instances of a word in visual basc

when i use

RichTextBox1.SelectionStart = RichTextBox1.Find("println.")
RichTextBox1.SelectionColor = Color.Red

it only colors one of the words println. so i was wondering how i could color multiple instances of the word println. this was a thought.

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

but im not sure thats going to work

Upvotes: 3

Views: 74

Answers (3)

Reese Houseknecht
Reese Houseknecht

Reputation: 131

Thank you for the help

  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

this was what i used and it works great for what im doing, it colors all instances of the word println.

Upvotes: 1

Mark
Mark

Reputation: 8160

To expand on Nico's comment a little, there is an overload of Find that allows you to specify the start position, so you need to track the current position, remembering to skip past the text that was found so you don't find the same occurrence repeatedly:

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

Note: Updated as discussed in the comments, although I'm not entirely sure I understand why RTB seems to restart the search when you pass in a start equal to the length of the text.

Update: Looking at the source code for RichTextBox.Find(String, Int32, RichTextBoxFinds) shows that if you pass in start equal to the length of the string and end equal to -1 (the default for this overload) then the search range will be reset to include the entire text, thereby restarting the search and resulting in the infinite loop spotted by @RawN. I don't understand why it works like this, but it explains the need for the additional AndAlso start < RichTextBox1.Text.Length check.

Upvotes: 0

soohoonigan
soohoonigan

Reputation: 2350

You can use a loop that will keep running as long as instances of the word are found, and at the end of each loop, set your start point for IndexOf to the end of the word you just highlighted. This way, the loop essentially moves through the string and ignores areas it has already searched in previous iterations. Here's an example of the concept:

    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

The above changes the rtb's text from:

one two three one two three one two three

to:

one two three one two three one two three

Upvotes: 0

Related Questions