Click here to Skip to main content
15,888,968 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I am trying to create a Find function in a richTextBox object,
So... the child form is the find form,
and the parent form is the richTextBox's form, and i want to transfer a simple integer from the child to the parent, is it possible anyhow...?

Form1.cs's event - when clicking on the find option in the main form
C#
private void findToolStripMenuItem_Click(object sender, EventArgs e)
{
    string textBlock = richTextBox1.Text;
    Form2 f = new Form2(textBlock);
    f.Show();
    //--------------------------------------------------//
    // if(the button was clicked and the word was found)
    //--------------------------------------------------//
    {
        richTextBox1.Focus();
        richTextBox1.Select(f.getLocation(), f.getEndLocation());
    }
}


Form2.cs's event-when clicking on the find button after you wrote the keyword
C#
private void find_btn_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Length == 0) { }
            else
                if (this.textBlock.Length == 0)
                { MessageBox.Show("Cannot find '" + textBox1.Text + "'"); }
                else
                {
                    Boolean flag = false;
                    int i;
                    string[] Arr = textBox1.Text.Split('\n');
                    for (i = 0; (i < Arr.Length && !flag); i++)
                    {
                        flag = (findInContext(Arr[i]));
                    }
                    if (flag)
                    {
                        MessageBox.Show("The item was found on line " + i);
                        this.location = this.textBlock.IndexOf(textBox1.Text);
                        this.endLocation = this.location + textBox1.Text.Length;
                    }
                    else
                    MessageBox.Show("Cannot find '" + textBox1.Text + "'");
                }
        }


By the way - if you have any better algorithms for solving this problem- i'll be grateful if you post them...
Posted
Updated 23-Mar-11 10:56am
v3

Forms cannot be child and parent: they only can be owner and owned, which is a huge difference. I also advice you should always support ownership (even though it is optional). It helps the user while doing Ctrl+Alt+Tab.

For passing data, you should use Form property. The setter of the property (like integer one) can also reflect the change in the form appearance. As an advanced technique (good in more complex cases) use some interface implemented by the form receiving data. It absolutely does not matter which form is the owner of which.

—SA
 
Share this answer
 
Comments
fcronin 23-Mar-11 19:56pm    
Agree on form property, best bet.
Sergey Alexandrovich Kryukov 23-Mar-11 20:01pm    
Thank you.
--SA
Toli Cuturicu 24-Mar-11 9:22am    
Forms can be MDI Child and MDI Parent, though.
Sergey Alexandrovich Kryukov 24-Mar-11 12:08pm    
Oh, sure. I didn't even consider those. You see, several times I already denied to answer any questions on MDI. Instead I write "Never use MDI!" :-)
Thank you very much for the note, you're right.
--SA
If you need the integer in the owner, the cleanest way I can think of is to create a public member on the 'owned' form, then access that once the find form is closed.

If you need the integer without closing the find form, research raising events and consume that event on the owner.

Here's what I normally do with search functionality... I like to show in the original control where the text is found as it is found... so, I would pass a reference of the richeditbox in the find form's constructor... then you can highlight where the text was found etc in the control itself.

Hope that gives you an idea of your options at least.
 
Share this answer
 
Comments
fcronin 23-Mar-11 19:52pm    
Sorry, first point was made by SA... I just second it! :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900