Control for Find text in RichTextBox C#

Hemanth B 886 Reputation points
2021-06-04T16:30:29.957+00:00

Hi, I created a notepad in C#. I added a button named "Search this Rich Text Box". Now I want to implement the code for the search button that search and highlight the text in the rich textbox. Please give the code for that.

Developer technologies C#
{count} votes

Accepted answer
  1. Anonymous
    2021-06-23T14:55:23.743+00:00

    Hello,

    This will start the search again automatically:

    private int Start = -1;
            private void FindText()
            {
                int Index = richTextBox1.Text.IndexOf(txtSearchWord.Text, Start + 1, StringComparison.OrdinalIgnoreCase);
    
                if (richTextBox1.Text.ToUpper().Contains(txtSearchWord.Text.ToUpper()))
                {
                    if (Index != -1)
                    {
                        richTextBox1.Focus();
                        richTextBox1.SelectionStart = Index;
                        richTextBox1.SelectionLength = txtSearchWord.Text.Length;
                        richTextBox1.ScrollToCaret();
                        Start = Index;
                    }
                    else
                    {
                        Start = -1;
                        Index = richTextBox1.Text.IndexOf(txtSearchWord.Text, Start + 1, StringComparison.OrdinalIgnoreCase);
                        richTextBox1.Focus();
                        richTextBox1.SelectionStart = Index;
                        richTextBox1.SelectionLength = txtSearchWord.Text.Length;
                        richTextBox1.ScrollToCaret();
                        Start = Index;
                    }
                }
                else
                {
                    MessageBox.Show("The following text [" + txtSearchWord.Text + "] was not found.");
                }
            }
    

    I hope it helps.


2 additional answers

Sort by: Most helpful
  1. Anonymous
    2021-06-04T18:38:43.22+00:00

    Hello,

    You can try this code:

    private int Start = -1;
    
            private void FindText()
            {
                int Index = richTextBox1.Text.IndexOf(txtSearchWord.Text, Start + 1, StringComparison.OrdinalIgnoreCase);
    
                if (richTextBox1.Text.ToUpper().Contains(txtSearchWord.Text.ToUpper()))
                {
                    if (Index != -1)
                    {
                        richTextBox1.Focus();
                        richTextBox1.SelectionStart = Index;
                        richTextBox1.SelectionLength = txtSearchWord.Text.Length;
                        richTextBox1.ScrollToCaret();
                        Start = Index;
                    }
                    else
                    {
                        MessageBox.Show("Found all the instances of [" + txtSearchWord.Text + "]", "End Search");
                        Start = -1;
                    }
                }
                else
                {
                    MessageBox.Show("The following text [" + txtSearchWord.Text + "] was not found.");
                }
            }
    

    This code assumes that the RichtextBox is named: richTextBox1 and the Search Textbox is named: txtSearchWord. To change the search to non-case sensitive, change this line:

    int Index = richTextBox1.Text.IndexOf(txtSearchWord .Text, Start + 1, StringComparison.Ordinal);
    

    To:

    int Index = richTextBox1.Text.IndexOf(txtSearchWord .Text, Start + 1, StringComparison.OrdinalIgnoreCase);
    

    I hope it helps.

    1 person found this answer helpful.

  2. Daniel Zhang-MSFT 9,651 Reputation points
    2021-06-07T08:10:46.69+00:00

    Hi HemanthB-9452,
    You can use RichTextBox.Find method to searche for text within the contents of the RichTextBox.
    Here is my test code you can rfer to.

    private void Form1_Load(object sender, EventArgs e)  
    {  
        richTextBox1.Text = "Copy and paste the paragraph in Rich TextBox. Enter the text in the Textbox you need to search and highlight in the paragraph. After clicking the search Button, the Text entered in the Textbox will be searched and highlighted in the paragraph.";  
    }  
      
    private void Search_Click(object sender, EventArgs e)  
    {  
        string[] words = textBox1.Text.Split(',');  
        foreach (string word in words)  
        {  
            int startindex = 0;  
            while (startindex < richTextBox1.TextLength)  
            {  
                int wordstartIndex = richTextBox1.Find(word, startindex, RichTextBoxFinds.None);  
                if (wordstartIndex != -1)  
                {  
                    richTextBox1.SelectionStart = wordstartIndex;  
                    richTextBox1.SelectionLength = word.Length;  
                    richTextBox1.SelectionBackColor = Color.Yellow;  
                }  
                else  
                    break;  
                startindex += wordstartIndex + word.Length;  
            }  
        }  
    }  
      
    private void Clear_Click(object sender, EventArgs e)  
    {  
        richTextBox1.SelectionStart = 0;  
        richTextBox1.SelectAll();  
        richTextBox1.SelectionBackColor = Color.White;  
    }  
    

    The result:
    102971-67.gif
    Best Regards,
    Daniel Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.