views:

102

answers:

6

Hello everyone, 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.

A: 

Your textbox on the new instance of frm1 will have no value surely? So there is nothing for the method to do...

Try stepping through the code and checking you actually have values to work with?

When you say:

Form1 frm1 = new Form1();

You are creating a fresh version so any extra information thats been added you dont have when accessing frm1

Try this and you will see what I mean

Form1 frm1 = new Form1();
frm1.Show();

When this code is excecuted you will see that you have actually made another instance of your form.

What you need to do is work with the original instance rather than create a new one, so that you still have all that information in your textboxes.

I'll leave you to work this one out, but there is you answer :)

Yoda
Your correct, I just added a messagebox to return frm2.textbox1.Text and it was blank. How can I fix this?EDIT* I got it to return a value but it still isn't working
Tanner
@Tanner: You need to give `Form2` the original instance of `Form1`, and vice-versa. See my answer.
SLaks
A: 

By writing Form1 frm1 = new Form1();, you're creating a brand-new instance of the Form1 form, which never gets any text and is never shown to the user.

You need to pass the original Form1 instance to Form2 in Form2's constructor.

Similarly, when you write Form2 frm2 = new Form2(); in FindNext, you're making a brand-new Form2 instance without any text.
Instead, you should pass the text as a parameter to the FindNext method.

For example:

public void FindNext(string searchText) {
    ...
    findPos = richTextBox1.Find(searchText, findPos, RichTextBoxFinds.None);
    ...
}

originalForm.FindNext(textBox1.Text);
SLaks
A: 

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); 
} 
Adam Robinson
A: 

Looks like you are referencing two different instances of Form2.

In your Form1.FindNext() you have a new instance of Form2 that you are creating and getting the text value from which is different to the instance where you are calling your FindNext() from.

What you might want to do is to pass in the instance of the form to FindNext(). So your function would be...

//Form1
 public void FindNext(Form2 frm2)
{
    try
    {
        this.Focus();
        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(this);
 }
ilias
A: 

Thanks Guys, this really helped!

Tanner
Press the accept tick on someones answer, people like that sort of thing and it looks good when you come back to ask another question ;)
Yoda
You should click the hollow check mark next to the answer that was most helpful to accept that answer.
SLaks
A: 
  string s = Interaction.InputBox("enter search text", "Notepad-search", "", 100, 100);
    //The above syntax is from vb.net so add reference as microsoft.VisualBasic from   references. The above code creates an alertbox. Then type the text which you want search and click on ok.

           int f = richTextBox1.Find(s);
           if (f >= 0)
           {
               MessageBox.Show("search Text is found");
           }
           else
           {
               MessageBox.Show("search Text is not found");
           }
pavan