在两个打开的表单之间传输数据-从子级到父级 [英] Transfering data between two open forms - from a child to a parent

查看:77
本文介绍了在两个打开的表单之间传输数据-从子级到父级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在richTextBox对象中创建Find函数,
所以...子表单就是查找表单,
父窗体是richTextBox的窗体,我想将一个简单的整数从子窗体传递到父窗体,是否有可能...?

Form1.cs的事件-在主窗体中单击查找"选项时

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

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的事件-编写关键字后单击查找按钮时



Form2.cs''s event-when clicking on the find button after you wrote the keyword

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...

推荐答案

表格无法是孩子和父母:他们只能是所有者拥有,这是一个巨大的区别.我还建议您应始终支持所有权(即使它是可选的).它在执行Ctrl + Alt + Tab时可以帮助用户.

为了传递数据,您应该使用Form属性.属性的设置器(如整数1)也可以反映表单外观的变化.作为一种先进的技术(在更复杂的情况下很好),使用一些由表单接收数据的接口.哪种形式的所有者绝对不重要.

—SA
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


选中此

http://stackoverflow.com/questions/706326/sending- data-from-child-form-to-parent-form-textbox [
Check this

http://stackoverflow.com/questions/706326/sending-data-from-child-form-to-parent-form-textbox[^]


如果您需要所有者中的整数,那是我想到的最简洁的方法是在所有"表单上创建一个公共成员,然后在查找表单关闭后访问该成员.

如果您需要整数而不关闭查找表单,请研究引发事件并在所有者上使用该事件.

这是我通常使用搜索功能所做的...我想在原始控件中显示找到的文本所在的位置...因此,我将在搜索表单中传递richeditbox的引用'' s的构造函数...然后您可以突出显示控件本身中文本的位置等.

希望至少能让您对您的选择有所了解.
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.


这篇关于在两个打开的表单之间传输数据-从子级到父级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆