Thursday 15 May 2014

Transfer Values from one form to other form

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();
                         } 

No comments:

Post a Comment