Send Data Between Win Forms In C#

Many times, we need to send data from parent form to child form and vice versa. In the below blog, I will demonstrate both.
 
For this, you need to create a desktop application and 2 WinForms.
 
Send Data Between Win Forms In C# 
 
The above picture is form1.cs that represents the Parent form.
 
Here, we write a name and click on the button to send the data to the child form to get the age.
  1. private void send_btn_Click(object sender, EventArgs e)  
  2. {  
  3.     string name = name_tb.Text;  
  4.     Child form = new Child(name);  
  5.     form.ShowDialog();  
  6.     result_tb.Text = form.SendData();  
  7.              
  8. }  
You need to create an instance of the child form with 1 parameter constructor and send name to the child.
 
In Child.cs, you need to create a parameterized constructor.
  1. public Child (string name)  
  2. {  
  3.     InitializeComponent();  
  4.     name_tb.Text = name;  
  5. }  
And assign the name to the textbox which is present in the Child form.
 
Now, to send the Age back to parent form, you need to create a Public method in Child.cs.
  1. public string SendData()  
  2. {  
  3.     return "Age of " + name + " is " + age_tb.Text;  
  4. }  
Now, call this method in Form1.cs to get the value of Age.
  1. private void send_btn_Click(object sender, EventArgs e)  
  2. {  
  3.     string name = name_tb.Text;  
  4.     Child form = new Child(name);  
  5.     form.ShowDialog();  
  6.     result_tb.Text = form.SendData();  
  7.              
  8. }  
That's all.
 
Summary
 
To send data from Parent to Child form you need to create a parameterized constructor.
 
To send data from Child to Parent form you need to create a public method in Child class and use the method in Parent class to get the return value.
 
Watch the video tutorial by clicking on this link.