C# parameter transmission between the child form and the parent form

C# parameter transmission between the child form and the parent form

Recently, when working on a project, the problem of parameter transmission between the child form and the parent form was involved, and a method was summarized by consulting and learning.

1. Pass the child form to the parent form

Form1 is the main form and Form2 is the subform.

Implementation: Add a button1 on Form1, click button1 to display Form2, and then click button1 of Form2. Set the value of textBox2 of Form2 to textBox1 of Form1 through this.Owner in the button1_Click event (you can also pass a value in Form2) Give Form1, and then perform follow-up processing in Form1, just modify the sample code)

Sample code:

    //Form1上的代码(主窗体)
    public partial class Form1 : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            //显示Form2
            Form2 childForm = new Form2();
            childForm.Show();
            //定义Form2的“爸爸”为Form1 
            calForm.Owner = this;
            

            或者
            //Form2 childForm = new Form2();
            //childForm.Show(this);
        }
    }


    //Form2上的代码(子窗体)
    public partial class Form2 : Form
    {
        public CalibrationForm()
        {
            InitializeComponent();
        }
        private void button_Click(object sender, EventArgs e)
        {
            MainForm mForm = (MainForm)this.Owner;
            //注意 如果textBox1是放在panel1中的 则先找panel1 再找textBox1
            ((TextBox)mForm.Controls["textBox1"]).Text = this.textBox2.Text;

            //也可直接将控件的属性Modifiers修改为public 然后直接调用
            mForm.textBox1.Text = this.textBox2.Text;
        }
     }

2. Pass the parent form to the child form

Form1 is the main form and Form2 is the subform.

Implementation: Add a button1 to Form1, click button1 to display Form2, and then click button1 of Form2 to display a certain parameter in Form1. (Use the constructor to instantiate the Form2 form, and then pass in the this pointer of Form1, so that you can call the parameters of Form1 in Form2 ( this parameter must be a public attribute ))

Sample code:

    //Form1上的代码(主窗体)
    public partial class Form1 : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }
        
        public string str = "你好!";
        private void button1_Click(object sender, EventArgs e)
        {
            //实例化子窗体,并将父窗体的指针this传入
            Form2 childForm = new Form2(this);
            childForm.Show();
        }
    }


    //Form2上的代码(子窗体)
    public partial class Form2 : Form
    {
        public CalibrationForm()
        {
            InitializeComponent();
        }

        private Form1 frmMain;
        public Form2(Form1 mForm)
        {
            InitializeComponent();
            this.frmMain = mForm;
        }

        private void button1_Click(object sender,EventArgs e)

        {

             this.textBox1.Text = mForm.str;  

        }
     }

The parameter transmission method between the child form and the parent form is not limited to this one. You can also use delegates. Here I only summarize a method that I think is relatively simple. If delegates are involved in the future, they will be supplemented.

Guess you like

Origin blog.csdn.net/Kevin_Sun777/article/details/109008722