0

I have this class:

private class Utility
        {
            public static void HighlightText(RichTextBox myRtb, string word, Color color)
            {
                int s_start = myRtb.SelectionStart, startIndex = 0, index;

                while ((index = myRtb.Text.IndexOf(word, startIndex)) != -1)
                {
                    myRtb.Select(index, word.Length);
                    myRtb.SelectionColor = color;

                    startIndex = index + word.Length;
                }

                myRtb.SelectionStart = s_start;
                myRtb.SelectionLength = 0;
                myRtb.SelectionColor = Color.Black;
            }
        }

Then using it:

private void textBox1_TextChanged(object sender, EventArgs e)
        {
            Utility.HighlightText(richTextBox1, textBox1.Text, Color.Red);
        }

First time when i click something in the textbox it will color the text in the richTextBox. But then when i delete the text in the textBox and it's empty the program freeze and i used a breakpoint it seems like it stuck in the while loop in the Utility class in the HighlightText.

What i want to do is:

  1. Type text free in thextBox also delete the text and type new text over again and it will highlight the text from the textBox in the richTextBox in real time.

  2. Only when i type words in the textBox then highlight/color it in the richTextBox. Now if i type for example the word SHARE it will also highlight/color also all the places that contain or start with the letter S.

1
  • Looks like there's cases where myRtb.Text.IndexOf(word, startIndex) won't be -1
    – JSelser
    May 31, 2015 at 23:02

2 Answers 2

1

You just need to take into account the case where the word to highlight will be empty, in which case, you simply want to skip the loop. Otherwise, it will loop indefinitely, because startIndex never increments, because you keep adding zero to it (word.Length).

So just add a condition around the loop like so:

public static void HighlightText(RichTextBox myRtb, string word, Color color)
{
    int s_start = myRtb.SelectionStart, startIndex = 0, index;

    if (!string.IsNullOrEmpty(word)) {
        while ((index = myRtb.Text.IndexOf(word, startIndex)) != -1)
        {
            myRtb.Select(index, word.Length);
            myRtb.SelectionColor = color;

            startIndex = index + word.Length;
        }
    }

    myRtb.SelectionStart = s_start;
    myRtb.SelectionLength = 0;
    myRtb.SelectionColor = Color.Black;
}

EDIT: I should also add that when word is empty, then the myRtf.Text.IndexOf(word, startIndex) method call will always return the value of startIndex. It will never return -1. All the more reason to skip the loop in that case.

0

Without going deeper into details, the simplest solution will be adding try-catch block to your method as shown below:

            public static void HighlightText(RichTextBox myRtb, string word, Color color)
            {
                try{
int s_start = myRtb.SelectionStart, startIndex = 0, index;

                while ((index = myRtb.Text.IndexOf(word, startIndex)) != -1)
                {
                    myRtb.Select(index, word.Length);
                    myRtb.SelectionColor = color;

                    startIndex = index + word.Length;
                }

                myRtb.SelectionStart = s_start;
                myRtb.SelectionLength = 0;
                myRtb.SelectionColor = Color.Black;
} catch{}
            }

Hope this may help. Best regards,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.