SoFunction
Updated on 2025-03-01

C# simple method to pass values ​​to parent form

This article describes the method of simply implementing the child form to pass values ​​to the parent form in C#. Share it for your reference. The details are as follows:

Click button1 of Form1 to open Form2
Click on the button2 of Form2 again

In button2_Click event, set the value of Form2's textBox2 to Form1's textBox1
And turn off Form2

public partial class Form1 : Form
{
  public Form1()
  {
   InitializeComponent();
  }
  private void button1_Click(object sender, EventArgs e)
  {
   Form2 frm2 = new Form2();
   (this);//or (this);   ////or   //Form2 frm2 = new Form2();
   // = this;
   //();//or ();  }
}
public partial class Form2 : Form
{
  public Form2()
  {
   InitializeComponent();
  }
  private void button2_Click(object sender, EventArgs e)
  {
   Form1 frm1 = (Form1);
  //Note if textBox1 is placed in panel1, then look for panel1 first and then textBox1   ((TextBox)["textBox1"]).Text = this.;
   ();
  }
}

Click on button1 of Form1 to open Form2
Click on the button2 of Form2 again
In the button2_Click event, the public property or method of the parent form Form1 is called and called

Set the value of textBox2 of Form2 to textBox1 of Form1
And turn off Form2

public partial class Form1 : Form
{
  public Form1()
  {
   InitializeComponent();
  }
  public string TextBox1Text
  {
   set { this. = value; }
   get { return this.; }
  }
  private void button1_Click(object sender, EventArgs e)
  {
   Form2 frm2 = new Form2();
   (this);//or (this);   ////or   //Form2 frm2 = new Form2();
   // = this;
   //();//or ();  }
}
public partial class Form2 : Form
{
  public Form2()
  {
   InitializeComponent();
  }
  private void button2_Click(object sender, EventArgs e)
  {
   Form1 frm1 = (Form1);
   frm1.TextBox1Text = this.;
   ();
  }
}

I hope this article will be helpful to everyone's C# programming.