How to Search and Highlight Text in a RichTextBox using C#

By FoxLearn 11/19/2024 2:32:02 PM   10.55K
In C#, you can search and highlight text in a RichTextBox by using the Find method to locate the text and then modifying the selection or applying formatting to highlight the found text.

Open Visual Studio, then click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "SearchRichTextBox" and then click OK

Drag and drop the Label, TextBox, Button and RichTextBox control from Visual Toolbox onto your form designer, then design your form as below.

highlight text in richtextbox

How to search for a specific text and highlight it in a RichTextBox in C#?

First, Use the Find method to search for the text, then use the SelectionBackColor property to change the background color of the found text to highlight it.

 

 

private void btnSearch_Click(object sender, EventArgs e)
{
    // Split a string to arrays
    string[] words = txtSearch.Text.Split(',');
    foreach (string word in words)
    {
        // Start searching from the beginning of the document
        int startIndex = 0;
        // Start searching and highlighting
        while (startIndex < richTextBox.TextLength)
        {
            // Find the next occurrence of the search term starting from startIndex
            int wordStartIndex = richTextBox.Find(word, startIndex, RichTextBoxFinds.None);
            if (wordStartIndex != -1)
            {                
                // Restore the cursor position
                richTextBox.SelectionStart = wordStartIndex;
                richTextBox.SelectionLength = word.Length;
                // Highlight the found text by changing the selection back color
                richTextBox.SelectionBackColor = Color.Yellow;
            }
            else
                break; // If no more matches are found, break the loop
            // Move the start index to the end of the current match to find the next match
            startIndex += wordStartIndex + word.Length;
        }
    }
}

private void btnClear_Click(object sender, EventArgs e)
{
    // Clear any previous highlights
    richTextBox.SelectionStart = 0;
    richTextBox.SelectAll();
    richTextBox.SelectionBackColor = Color.White;
}

The Find method is used to find the searchTerm in the RichTextBox. It returns the starting index of the found text or -1 if the text is not found.

After finding a match, SelectionBackColor is used to change the background color of the selected text, effectively highlighting it.

The loop continues searching from the end of the last found text to highlight all occurrences of the search term in the RichTextBox.

If you want to ensure you're matching whole words, use RichTextBoxFinds.WholeWord in combination with Find.

For example:

int wordStartIndex = richTextBox.Find(searchTerm, startIndex, RichTextBoxFinds.MatchCase | RichTextBoxFinds.WholeWord);

This would make the search both case-sensitive and match only whole words.

VIDEO TUTORIAL