Tuesday, April 17, 2012

Passing Data Between two forms (Windows forms using c#)


In this post Am going to explain about how to pass the variables between two windows forms . In many situations of developing the winforms I have faced this problem so I am explaining this :
The following are the 4 methods you can use and pass the variables from one form to other form .They are
                
  1. Using constructor
  2. Using objects
  3. Using properties
  4. Using delegates
To demonstrate this please create two forms in the windows application With names “Form1” and “form2” with the following specifications :

Step 1: Create new project and select windows application. This will create a default form as "Form1". We can use this form for sending data.

Step 2: Add a textbox and a button to the form.

Step 3: Add another windows form for receiving the data and to display it. Right click the project and select Add->Add Windows Form. Enter a name or use the default name "Form2.cs" and click ok button.

Step 4: Add a label to the second form to display the text from form1


The Constructor Approach
It is very easiest and simple approach. A method is invoked whenever you instantiate an object. This method is called a constructor. Code a constructor for form2 class with one string parameter. In the constructor assign the text to the label's text property. Instantiate form2 class in form1's button click event handler using the constructor with one string parameter and pass the textbox's text to the constructor.
Follow the below steps.

Step 1: Code a constructor for form2 class as below 

public Form2(string strTextBox)
        {
            InitializeComponent();
            label1.Text = strTextBox;
        }
Step 2: Instantiate form2 class in form1's button click event handler as below

private void button1_Click(object sender, EventArgs e)
        {
            Form2 frm = new Form2(textBox1.Text);
            frm.Show();


        }

The Object Approach
Object is nothing but the istance of a class.These are the Refrenced type variables and store in the Heap . Here we are going to pass data using objects. The approach is simple; in form2 we are going to instantiate form1 class.
Then instantiate form2 in the button click event handler of form1. After this we are going to pass form1 object to the form2 using form2's form1 object. The last step is to invoke the form2 window by calling the form2's show method.

Follow the below steps:

Step 1: Change the access modifier for textbox in form1 to public 
 public class Form1 : System.Windows.Forms.Form
 {
                   public System.Windows.Forms.TextBox textBox1;

Step 2: In the button click event-handler add the following code. 
private void button1_Click(object sender, EventArgs e)
        {
            Form2 frm = new Form2();
            frm.frm1 = this;
            frm.Show();


        }

Step 3:  In form2.cs, instantiate form1 class 
 public class Form2 :           System.Windows.Forms.Form
 {
            private System.Windows.Forms.Label label1;
        public Form1 frm1;

Step 4: In Form2's Load method type cast the object (frm1) of form1 to Form1 and access form1's textbox and assign its text to label's text. 
private void Form2_Load_1(object sender, EventArgs e)
        {
            label1.Text = ((Form1)frm1).textBox1.Text;
        }

 
The Properties Approach

Properties allow clients to access class state as if they were accessing member fields directly, while actually implementing that access through a class method. In this method we are going to add one property to each form. In form1 we are going to use one property for retrieving value from the textbox and in form2, one property to set the label's text property. Then, in form1's button click event handler we are going to instantiate form2 and use the form2's property to set the label's text.

Follow the below steps:

Step 1: Add a property in form1 to retrieve value from textbox. 
 public string _textBox1
 {
                             get{return textBox1.Text;}
 } 
Step 2: Add a property in form2 to set the labels' text 
 public string _textBox
 {
                             set{label1.Text=value;}
 } 
Step 3: In form1's button click event handler add the following code. 
 private void button1_Click(object sender, System.EventArgs e)          
             {
                              Form2 frm=new Form2();
                             frm._textBox=_textBox1;
                             frm.Show();
                         } 


The Delegates Approach


Technically, a delegate is a reference type used to encapsulate a method with a specific signature and return type. You can encapsulate any matching method in that delegate. Here we are going to create a delegate with some signature and assign a function to the delegate to assign the text from textbox to label.

Follow the below steps:

Step 1: Add a delegate signature to form1 as below 
   public delegate void delPassData(TextBox text);

Step 2: In form1's button click event handler instantiate form2 class and delegate. Assign a function in form2 to the delegate and call the delegate as below 
private void button1_Click(object sender, EventArgs e)
        {
            Form2 frm = new Form2();
            delPassData del = new delPassData(frm.funData);
            del(this.textBox1);
            frm.Show();

        }
Step 3: In form2, add a function to which the delegate should point to. This function will assign textbox's text to the label. 
 public void funData(TextBox txtForm1)
        {
            label1.Text = txtForm1.Text;
        }

 
Output :







2 comments:

  1. Hi check the below link. this will help you.




    http://dotnetpools.com/Article/ArticleDetiail/?articleId=47&title=Code%20To%20Pass%20Value%20From%20One%20Windows%20Form%20To%20Another%20Windows%20%20Form%20In%20Windows%20Form%20Using%20C#

    ReplyDelete