Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think you may be confused in how you reference <code>Form1</code> and <code>Form2</code> from each other. </p> <p>Calling <code>new Form()</code> and <code>new Form2()</code> create references to <em>new</em> instances of <code>Form1</code> and <code>Form2</code>, they don't reference the forms that are already open. You need to get the references for the <em>existing</em> instances.</p> <p>Assuming that <code>Form1</code> is the main form for your application and it creates and shows <code>Form2</code>, you can either add a property to <code>Form2</code> that represents the instance of <code>Form1</code> that created it, or you can appropriate the <code>Owner</code> property for this purpose (I'd recommend that).</p> <p>In your code on <code>Form1</code> that shows <code>Form2</code> initially (not in the code you have above), call <code>frm2.Show(this)</code> instead of just <code>frm2.Show()</code>. This will set the <code>Owner</code> property of your <code>Form2</code> instance equal to thinstance of <code>Form1</code> that opened it.</p> <p>Then change your button code for <code>Form2</code> to this:</p> <pre><code>private void button1_Click(object sender, EventArgs e) { Form1 frm1 = (Form1)Owner; frm1.FindNext(); } </code></pre> <p>This will make you reference the existing form rather than a new one, which is what you want.</p> <p>As far as the <code>FindNext</code> function goes, you have two choices: either you can hold on to the reference of <code>Form2</code> (though you probably want to do this anyway) and access the text directly, or you can change <code>FindNext</code> to take a string (this is what I'd recommend).</p> <pre><code>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; } } </code></pre> <p>Then change the call to <code>frm1.FindNext()</code> on <code>Form2</code> to <code>frm1.FindNext(textBox1.Text)</code>:</p> <pre><code>private void button1_Click(object sender, EventArgs e) { Form1 frm1 = (Form1)Owner; frm1.FindNext(textBox1.Text); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload