How to search for text in a rich text box
<p>I have this code for finding text in the Rtf box and highlighting the text.</p>
<pre><code>public Form1()
{
InitializeComponent();
}
public int currentPos = 1; // this is so currentPos does not loose its value
</code></pre>
<p>Then I have Three events tied to buttons like this</p>
<pre><code>private void button5_Click(object sender, EventArgs e)
    {
       
        currentPos = 1;

        if (!string.IsNullOrEmpty(this.textBox1.Text))
        {
            if (this.richTextBox1.Text.Contains(this.textBox1.Text) &amp;&amp; currentPos &lt; this.richTextBox1.Text.Length)
            {
                if (this.richTextBox1.Text.Substring(currentPos).Contains(this.textBox1.Text))
                {

                    int start = this.richTextBox1.Text.Substring(currentPos).IndexOf(this.textBox1.Text);
                    this.richTextBox1.Select(start + currentPos, this.textBox1.Text.Length);
                    currentPos = start + currentPos + this.textBox1.Text.Length;
                    this.richTextBox1.Focus();
                }
                else
                {
                    //restart the method after resetting the indicator
                    button5.Text = &quot;Find Next&quot;;
                    button5_Click(button5, new EventArgs());
                }
            }
            else
            {
                //restart the method after resetting the indicator
                currentPos = currentPos + 1;
                if (this.richTextBox1.Text.Contains(this.textBox1.Text))
                {
                 
                    button5_Click(button5, new EventArgs());
                }
                else
               
                    MessageBox.Show(&quot;Text not found&quot;);
            }
        }
    
     }

    // same as the previous code for button5_Click except currentPos
    // does not start over, it keeps searching from where it found the
    // last text
    private void button6_Click(object sender, EventArgs e)
    {
             if (!string.IsNullOrEmpty(this.textBox1.Text))
        {
            if (this.richTextBox1.Text.Contains(this.textBox1.Text) &amp;&amp; currentPos &lt; this.richTextBox1.Text.Length)
            {
                if (this.richTextBox1.Text.Substring(currentPos).Contains(this.textBox1.Text))
                {

                    int start = this.richTextBox1.Text.Substring(currentPos).IndexOf(this.textBox1.Text);
                    this.richTextBox1.Select(start + currentPos, this.textBox1.Text.Length);
                    currentPos = start + currentPos + this.textBox1.Text.Length;
                    this.richTextBox1.Focus();
                }
                else
                {
                
                    DialogResult dialogResult = MessageBox.Show( &quot;Larry's Journal has finished searching through the document. Do you want to continue the search from the top of the document?&quot;, &quot;Message&quot;, MessageBoxButtons.YesNo);
                    if (dialogResult == DialogResult.Yes)
                    {
                        currentPos = 1;

                        button5_Click(button5, new EventArgs());
                    }
                    else if (dialogResult == DialogResult.No)
                    {
                        //do something else
                    }

                }
            }
           else
            {
                //restart the method after resetting the indicator
                currentPos = currentPos + 1;
                if (this.richTextBox1.Text.Contains(this.textBox1.Text))
                    button1_Click(button5, new EventArgs());
                else
                    MessageBox.Show(&quot;Text not found&quot;);
            }
        }

    }
 // This replaces the selected text in the richtextbox with the contents
 // of texxtBox2 which is the replacement text.
 private void button10_Click(object sender, EventArgs e)
    {
       textBox2.SelectAll();
        textBox2.Copy();
        richTextBox1.Paste();
    }
</code></pre>
<p>The first button5_Click is for Finding the text in the richtextbox.
The second button6_Click is for finding the next instance of that text.
The Third button10_Click is for replacing the text found with some other text
if you so desire. I edited this question to show that I figured out how to do it. It's not the find methods Microsoft talks about in their page on how work with richtextboxes, but it works.</p>
<p>I post this here for any programmer who feels they can use it. Just copy and paste it into your program. All you need is 3 buttons and two textboxes and one richtextbox in addition to whatever other controls you already have in your program.</p>