Pass Data From One Form To Another Using Properties In C#

Initial chamber

Step 1: Open Visual Studio 2010, Go to File, New, Projects and 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 label change its text to name, email, city and then take three textboxes and a button. See the following 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.

design

Code chamber

Right click on the blank part of Form1.cs and 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.   
  10. namespace WindowsFormsApplication3  
  11. {  
  12.     public partial class Form1 : Form  
  13.     {  
  14.         public Form1()  
  15.         {  
  16.             InitializeComponent();  
  17.         }  
  18.   
  19.         private void button1_Click(object sender, EventArgs e)  
  20.         {  
  21.             Form2 frm2 = new Form2();  
  22.             frm2.passingvalue = textBox1.Text;  
  23.             frm2.passingvalue1 = textBox2.Text;  
  24.             frm2.passingvalue2 = textBox3.Text;  
  25.             frm2.ShowDialog();    
  26.   
  27.         }  
  28.   
  29.          
  30.     }  
  31. }  
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 WindowsFormsApplication3  
  11. {  
  12.     public partial class Form2 : Form  
  13.     {  
  14.         public string name;  
  15.         public string email;  
  16.         public string city;  
  17.   
  18.         public string passingvalue  
  19.         {  
  20.   
  21.             get { return name; }  
  22.             set { name = value; }      
  23.         }  
  24.   
  25.   
  26.         public string passingvalue1  
  27.         {  
  28.   
  29.             get { return email; }  
  30.             set { email = value; }  
  31.           
  32.           
  33.         }  
  34.   
  35.         public string passingvalue2  
  36.         {  
  37.   
  38.             get { return city; }  
  39.             set { city = value; }  
  40.           
  41.         }  
  42.   
  43.   
  44.         public Form2()  
  45.         {  
  46.             InitializeComponent();  
  47.         }  
  48.   
  49.         private void Form2_Load(object sender, EventArgs e)  
  50.         {  
  51.             label4.Text = "" + name;  
  52.             label5.Text = "" +email;  
  53.             label6.Text = "" + city;  
  54.         }  
  55.     }  
  56. }  
Output chamber

Output

Display Data on Form2

Display Data on Form2

Hope you liked this. Thank you for reading. Have a good day!

 

Up Next
    Ebook Download
    View all
    Learn
    View all