Passing Values Between TextBox of Different Forms Using Class

Introduction

 
In this article, I describe how to pass data from one form to another form in a Windows application using C#. When we encounter the requirement to use the first page's controls data on a second page in a Windows application it sometimes becomes difficult. Today I explore the possible ways to send the data from one page to another page in C# Windows application using a class.
 
Form1 description
 
In From1 we use two textboxes and two buttons. On button1 click first, we validate to ensure that the textboxes are not blank, calling the second form and saving the value of the textboxes in a variable declared in a class.
  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.Threading.Tasks;  
  9. using System.Windows.Forms;  
  10.   
  11. namespace WindowsFormsApplication1 {  
  12.     public partial class Form1: Form {  
  13.         public Form1() {  
  14.             InitializeComponent();  
  15.         }  
  16.         private void button2_Click(object sender, EventArgs e) {  
  17.             this.Close();  
  18.         }  
  19.         private void button1_Click(object sender, EventArgs e) {  
  20.             if (textBox1.Text == "" || textBox2.Text == "") {  
  21.                 MessageBox.Show("please don't leave any textbox is empty");  
  22.             } else {  
  23.                 Class1.name = textBox1.Text;  
  24.                 Class1.address = textBox2.Text;  
  25.                 Form2 ob = new Form2();  
  26.                 ob.Show();  
  27.             }  
  28.         }  
  29.     }  
Form2 description
 
In this form, we use two textboxes in which the values are displayed in Form1. We use one button for closing the Form2.
 
On the Form Load event, we define the coding of transferring the value in textbox1 and textbox2. On the button click event define the code for closing the form. 
  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.Threading.Tasks;  
  9. using System.Windows.Forms;  
  10.   
  11. namespace WindowsFormsApplication1 {  
  12.     public partial class Form2: Form {  
  13.         public Form2() {  
  14.             InitializeComponent();  
  15.         }  
  16.         private void Form2_Load(object sender, EventArgs e) {  
  17.             textBox1.Text = Class1.name;  
  18.             textBox2.Text = Class1.address;  
  19.         }  
  20.         private void button1_Click(object sender, EventArgs e) {  
  21.             this.Close();  
  22.         }  
  23.     }  
Class description
 
In this class, we use two static string variables and create two static functions to set and get the value of these two variables.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace WindowsFormsApplication {  
  8.   class Class1 {  
  9.     static string nm, add;  
  10.     public static string name {  
  11.       get {  
  12.         return nm;  
  13.       }  
  14.       set {  
  15.         nm = value;  
  16.       }  
  17.     }  
  18.     public static string address {  
  19.       get {  
  20.         return add;  
  21.   
  22.       }  
  23.       set {  
  24.         add = value;  
  25.       }  
  26.     }  
  27.   }  
Output
 
pic 1.jpg
 
pic 2.jpg
 
pic 4.jpg


Recommended Free Ebook
Similar Articles