如何在两个表单之间传递文本框数据? [英] How to pass textbox data between two forms?

查看:31
本文介绍了如何在两个表单之间传递文本框数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过按钮在没有 Show()/ShowDialog() 的情况下将文本框值发送到两个表单之间的文本框?我想让 textBox 在没有打开表单的情况下获得价值.

How to send textbox value to textbox between two forms without Show()/ShowDialog() by button? I want to textBox will get value without open form.

推荐答案

要将信息从父表单传递到子表单,您应该在子表单上为它需要接收的数据创建一个属性,然后让父表单设置该属性(例如,在按钮点击时).

To pass information from a parent from to a child form you should create a property on the child form for the data it needs to receive and then have the parent form set that property (for example, on button click).

要让子表单向父表单发送数据,子表单应该创建一个属性(它只需要是一个 getter),其中包含要发送给父表单的数据.然后它应该创建一个父级可以订阅的事件(或使用现有的 Form 事件).

To have a child form send data to a parent form the child form should create a property (it only needs to be a getter) with the data it wants to send to the parent form. It should then create an event (or use an existing Form event) which the parent can subscribe to.

示例:

namespace PassingDataExample
{
    public partial class ParentForm : Form
    {
        public ParentForm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ChildForm child = new ChildForm();
            child.DataFromParent = "hello world";

            child.FormSubmitted += (sender2, arg) =>
            {
                child.Close();

                string dataFromChild = child.DataFromChild;
            };

            child.Show();
        }
    }
}

namespace PassingDataExample
{
    public partial class ChildForm : Form
    {
        public ChildForm()
        {
            InitializeComponent();
        }

        public string DataFromParent { get; set; }

        public string DataFromChild { get; private set; }

        public event EventHandler FormSubmitted;

        private void button1_Click(object sender, EventArgs e)
        {
            DataFromChild = "Hi there!";

            if (FormSubmitted != null)
                FormSubmitted(this, null);
        }
    }
}

这篇关于如何在两个表单之间传递文本框数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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