Hello, I'm making a little app that runs through a Richtextbox and makes it turn string "[BK]" red.
But I can't get it to make all of the "[BK]"'s red.

i found this little piece of code, but I can't get it to work, it looks like it should though.

Dim textEnd As Integer = TextBox1.TextLength
            Dim index As Integer = 0
            Dim lastIndex As Integer = TextBox1.Text.LastIndexOf(TextBox1.Text)

            While index < lastIndex
                TextBox1.SelectionStart = TextBox1.Find(TextBox1.Text, index, textEnd, RichTextBoxFinds.None)
                TextBox1.SelectionColor = Color.Red
                index = TextBox1.Text.IndexOf("[BK]", index) + 1
            End While

Any help is appreciated:)

9 Years
Discussion Span
Last Post by Teme64

First, take a look at thread Search multiple words using Find method. That's about highlighting all occurrences of the given string.

Secondly, you have a control with a name TextBox1, which is the default name for the first text box control dropped on the form. First rich text box would be RichTextBox1 by default. Although text boxes don't have Find method or SelectionColor property, are you sure you're using a rich text box and not text box? (You do have Option Strict On and Option Explicit On, right?)


Its a richtextbox, i just renamed it for faster typing :P

Ill go try that link out and post the results later.


Ok, your code it that topic worked perfectly!

Dim FoundAtPosition As Integer
FoundAtPosition = RichTextBox1.Find("foo", RichTextBoxFinds.MatchCase)
Do Until FoundAtPosition < 0
  ' Highlight text
  ' Search next occurrence
  FoundAtPosition = RichTextBox1.Find("foo", FoundAtPosition + 1, RichTextBoxFinds.MatchCase)

Thanks a lot teme!


Hi! Nice to hear that you got answer to your problem. Could you please mark the thread as solved. Thank you! :)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.