Transfer Data from One Form to Another using C#

The value we want to transfer from one form to another,  say the value from child form to display in parent form, we can use amsimple way by declaring the public static variable in the child form. In the case of vise versa --  you want to pass the data to child form -- you can use the public static variable in parent form.

Step 1: Create Form1 windows application in the project. I have provided an example of calculating the total marks of the student. Click and drag the controls of labels, textboxes, and buttons. Design the form to your requirements as required.

Parent Form
                                       Form 1: Parent Form

Step 2: Create Form2 windows application in the projectClick and drag the controls of labels,textboxes and buttons.

Child Form
                                       Form 2: Child Form

Step 3: Create a public static class in child form where we are going to pass the data to parent form.

  1. public partial class Form2 : Form  
  2. {  
  3.     public static string StudTol;// Create a public static variable to pass the value  
  4.     public Form2()  
  5.     {  
  6.         InitializeComponent();  
  7.     }  
  8.   
  9.     private void Form2_Load(object sender, EventArgs e)  
  10.     {  
  11.   
  12.     }  
  13.   
  14.     private void btnTotal_Click(object sender, EventArgs e)  
  15.     {  
  16.         int sum = int.Parse(txtMAths.Text) + int.Parse(txtScience.Text) + int.Parse(txtSocial.Text);  
  17.         txtTotal.Text = sum.ToString();  
  18.    }  
  19.   
  20.     private void btnPassTotal_Click(object sender, EventArgs e)  
  21.     {  
  22.         Form1 objectData = new Form1();// create a object for parent form.  
  23.         StudTol = txtTotal.Text;//assign the data to the public  static variable.  
  24.         this.Close();  
  25.   
  26.     }  
  27.   
  28.     
  29. }  
Step 4:

In Form: Form2.cs
  1. public partial class Form1 : Form  
  2. {  
  3.     public Form1()  
  4.     {  
  5.         InitializeComponent();  
  6.     }  
  7.   
  8.     private void btnGetTotal_Click(object sender, EventArgs e)  
  9.     {  
  10.         Form2 objForm2 = new Form2();  
  11.         objForm2.ShowDialog();  
  12.         txtTol.Text = Form2.StudTol;// passing the data to the textbox.  
  13.     }  
  14.   
  15.     private void Form1_Load(object sender, EventArgs e)  
  16.     {  
  17.         display();  
  18.     }  
  19.   
  20.     private void display()  
  21.     {  
  22.        // throw new NotImplementedException();  
  23.         txtStudentName.Text = "";  
  24.         txtGender.Text = "";  
  25.   
  26.     }  
  27.   
  28.     private void btnClear_Click(object sender, EventArgs e)  
  29.     {  
  30.         txtGender.Clear();  
  31.         txtStudentName.Clear();  
  32.         txtTol.Clear();  
  33.   
  34.     }  
  35. }  
Note:
  1. Likewise, you can pass the data from one form to any number of forms by creating a public static variable and object to the form to which you are going to transfer the data.

  2. You can also create a .cs file as common and you can declare and define variables in that common class file and can access them anywhere in the project.