Pass Data From One Window Form To Another Window Form Application Using C#

We take some data from user in form 1 and display that data in form 2.

Initial chamber

Step 1: Open Visual Studio 2010, Go to File, New, Projects, then under Visual C# select Windows.

window form application

You can change the name of the project and browse your project to different location too. And then press – OK.

Design chamber

Step 2: Now open your Form1.cs file, where we create our design for form 1, take three labels, change its text to name, email, city and then take three textboxes and a button. Here's the image.

form 1

Now add one more Form to your project by going to your project- Right Click, Add New Item, then Window Form.

Make its design like the following image, here we have to take label control only to show the data from form1.

form 2

Code chamber

Right Click on the blank part of Form1.cs and click View Code. You will see you are entered in the code part of the form. Write the following code for Form1.cs.

Form1.cs

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. namespace WindowsFormsApplication1  
  10. {  
  11.     public partial class Form1: Form  
  12.     {  
  13.         public static string l1;  
  14.         public static string l2;  
  15.         public static string l3;  
  16.         public Form1()  
  17.         {  
  18.             InitializeComponent();  
  19.         }  
  20.         private void button1_Click(object sender, EventArgs e)  
  21.         {  
  22.             l1 = textBox1.Text;  
  23.             l2 = textBox2.Text;  
  24.             l3 = textBox3.Text;  
  25.             Form2 frm2 = new Form2();  
  26.             frm2.ShowDialog();  
  27.         }  
  28.     }  
  29. }  
Form2.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9.   
  10. namespace WindowsFormsApplication1  
  11. {  
  12.     public partial class Form2 : Form  
  13.     {  
  14.   
  15.         public Form2()  
  16.         {  
  17.             InitializeComponent();  
  18.   
  19.   
  20.             label4.Text = Form1.l1;  
  21.             label5.Text = Form1.l2;  
  22.             label6.Text = Form1.l3;  
  23.   
  24.               
  25.         }  
  26.          
  27.     }  
  28. }  
Output chamber

Output

Display data on Form2:

Display Data on Form2

Hope you liked this. Thank you for reading.