Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 812169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T01:09:43+00:00 2026-05-15T01:09:43+00:00

I am trying to make a find, find next function for my program, which

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T01:09:44+00:00Added an answer on May 15, 2026 at 1:09 am

    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); 
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to make this jQuery filter that uses .find case-insensitive. For example, when the
I am trying to make a program that will find similar images from a
I'm trying to find/make a good jquery drop-down menu with animation (fadein/fadeout or slidedown)
I'm trying to make a simple php script to find all src attributes from
I'm trying to make a Jquery plugin ( findFirst() ) to find the first
I'm trying to make a search feature that will search multiple columns to find
I'm trying to make a query, but I can't find a way to do
I'm trying to find a way to make a list of parent object with
i'm trying to find joomla plugins or another components to make joomla an OpenID
Hello i am trying to find out how can i print a file which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.