Search code examples
c#.netformsfind

Find, Find Next?


I am trying to make a find, find next function for my program, which I did manage to do with this code:

 int findPos = 0;
    private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            string s = textBox1.Text;
            richTextBox1.Focus();
            findPos = richTextBox1.Find(s, findPos, RichTextBoxFinds.None);
            richTextBox1.Select(findPos, s.Length);
            findPos += textBox1.Text.Length;
            //i = richTextBox1.Find(s, i + s.Length, RichTextBoxFinds.None);
        }
        catch
        {
            MessageBox.Show("No Occurences Found");
            findPos = 0;
        }
    }

And it works great in form1 but if I use this code and try to call it from form2 It doesn't do anything:

  //Form1
  public void FindNext()
    {
        try
        {
            this.Focus();
            Form2 frm2 = new Form2();
            string s = frm2.textBox1.Text;
            richTextBox1.Focus();
            findPos = richTextBox1.Find(s, findPos, RichTextBoxFinds.None);
            richTextBox1.Select(findPos + 1, s.Length);
            findPos += textBox1.Text.Length;
        }
        catch
        {
            MessageBox.Show("No Occurences Found");
            findPos = 0;
        }
    }

 //Form2
 private void button1_Click(object sender, EventArgs e)
    {
        Form1 frm1 = new Form1();
        frm1.FindNext();
    }

Does any one know why this is? Thanks,Tanner.


Solution

  • I think you may be confused in how you reference Form1 and Form2 from each other.

    Calling new Form() and new Form2() create references to new instances of Form1 and Form2, they don't reference the forms that are already open. You need to get the references for the existing instances.

    Assuming that Form1 is the main form for your application and it creates and shows Form2, you can either add a property to Form2 that represents the instance of Form1 that created it, or you can appropriate the Owner property for this purpose (I'd recommend that).

    In your code on Form1 that shows Form2 initially (not in the code you have above), call frm2.Show(this) instead of just frm2.Show(). This will set the Owner property of your Form2 instance equal to thinstance of Form1 that opened it.

    Then change your button code for Form2 to this:

    private void button1_Click(object sender, EventArgs e) 
    { 
        Form1 frm1 = (Form1)Owner;
        frm1.FindNext(); 
    } 
    

    This will make you reference the existing form rather than a new one, which is what you want.

    As far as the FindNext function goes, you have two choices: either you can hold on to the reference of Form2 (though you probably want to do this anyway) and access the text directly, or you can change FindNext to take a string (this is what I'd recommend).

    public void FindNext(string searchText)
    {
        try
        {
            this.Focus();
            richTextBox1.Focus();
            findPos = richTextBox1.Find(searchText, findPos, RichTextBoxFinds.None);
            richTextBox1.Select(findPos + 1, searchText.Length);
            findPos += searchText.Length;
        }
        catch
        {
            MessageBox.Show("No Occurences Found");
            findPos = 0;
        }
    }
    

    Then change the call to frm1.FindNext() on Form2 to frm1.FindNext(textBox1.Text):

    private void button1_Click(object sender, EventArgs e) 
    { 
        Form1 frm1 = (Form1)Owner;
        frm1.FindNext(textBox1.Text); 
    }