Using Properties Approach to Pass Data Between Forms

Introduction

There might be cases where you need to pass data from one form to another form in Windows Forms. The following are some of the approaches that can be adopted to pass data between forms.

  1. Constructor approach
  2. Using Objects
  3. Using Properties
  4. Delegate approach

Let us now see how to use the properties approach to pass data between forms.

Using Properties Approach

What are Properties in C#?


Properties in C# are nothing but the attributes that describe a class. They are like fields, and you can access properties in the same manner as accessing fields.

Then, you may ask what makes a difference between fields and properties?

Properties do not represent storage area as like fields. Instead, the properties contain accessor functions.

Defining a Property

In C#, we define a property using the following two steps:

  1. Define a private class member.
  2. Define the property using the get and set accessor functions.

Declaring a property

Here is an example of how we declare a property.

private string EmpName;
public string EName
{
      get
      {
           return EmpName;
      }
      set
      {
           EmpName = value;
      }
}

Accessor Functions

get: The get accessor function is used to read the value stored in the property.

set: The set accessor function is used to assign a value to the property. The value in the set function is an implicit parameter.

Now let us see how to use the properties approach to pass between forms.

  1. Create a new C# Windows Forms Project and name it "PassingDataBtForms".
     
  2. By default Form1 is added. Now add another form from "Project" -> "Add Windows Form". Click "Add" to add the form to the project and the structure of our project will then be:

    image1.gif
     
  3. Design your Form1 with the controls Label, TextBox, and Button. So our Form1 looks like this:

    image2.gif
     
  4. Here we pass the First Name from Form1 to Form2 and receive the input for the Last Name from Form2.

    Now Let us code Form1.

    Here in the Click Event of the OK button we send the First Name to Form2. The Form2 is designed like this:

    image3.gif
     
  5. Now the coding in Form1 to pass the First Name from Form1 to Form2.

    Form1 Coding:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms; 
    namespace PassingDataBtForms
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            private void btn_Ok_Click(object sender, EventArgs e)
            {
                Form2 frm = new Form2();
                frm.ReceiveFirstName = FName;
                frm.ShowDialog();
            } 
            public string FName
            {
                get
                {
                    return lbl_firstname.Text;
                }
            }
        }
    }

    Here we have declared a property to retrieve the value from lbl_firstname using the get accessor function.

    public string FName
    {
        get
        {
             return lbl_firstname.Text;
        }
    }

    In Form2, we add a property to set the label's text. The Label's text is set using the set accessor function.

    public string ReceiveFirstName
    {
         set
         {
               lbl_firstname.Text = value;
          }
    }

    Coding the Form2:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms; 

    namespace PassingDataBtForms
    {
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            } 
            public string ReceiveFirstName
            {
                set
                {
                    lbl_firstname.Text = value;
                }
            }
        }
    }
     

  6. Now having a close look at the event handler for the Button:

    private void btn_Ok_Click (object sender, EventArgs e)
    {
          Form2 frm = new Form2 ();
          frm.ReceiveFirstName = FName;
          frm.ShowDialog ();
     }

    Here, we retrieve the Labels text from the property FName and using the set accessor function in Form2, the value is set to Label at Form2.
     
  7. Now build and run the project. The Label at Form2 is set to Vinoth. Here it is!!
     
  8. Now we try to pass the Second Name from Form2 to Form1. In Form2 we define a property that returns the Second Name.

    public string SendSecondName
    {
          get
          {
               return txt_secondname.Text;
           }
    }
     
  9. In the Button Event Handler for Form2 we code the following:

    this.DialogResult = DialogResult.OK;
     
  10. Now, making a small change in our code for the Form1 Button Event Handler to receive the value only when the Button is clicked in Form2.

    Form 1 Button Event Handler :

    private void btn_Ok_Click(object sender, EventArgs e)
    {
          Form2 frm = new Form2();
          frm.ReceiveFirstName = FName;
          DialogResult rs = frm.ShowDialog();
          if (rs == DialogResult.OK)
             txt_secondname.Text = frm.SendSecondName;                
    }

    If the return value of the Dialog box is OK then the "txt_secondname.Text" value is set from the accessor defined in the Form2 (accessor SendSecondName).

image4.gif

Once the Ok button is clicked the Form2 is closed and the Second name is set in Form1 like this:

image5.gif

The complete code for Form1 and Form2 is the following.

Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace PassingDataBtForms
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        } 
        private void btn_Ok_Click(object sender, EventArgs e)
        {
            Form2 frm = new Form2 ();
            frm.ReceiveFirstName = FName;
            DialogResult rs = frm.ShowDialog ();
            if (rs == DialogResult.OK)
                txt_secondname.Text = frm.SendSecondName;                
        } 
        public string FName
        {
            get
            {
                return lbl_firstname.Text;
            }
        }
    }
}

Form2.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; 
namespace PassingDataBtForms
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        } 
        public string ReceiveFirstName
        {
            set
            {
                lbl_firstname.Text = value;
            }
        }
        private void btn_send_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }
        public string SendSecondName
        {
            get
            {
                return txt_secondname.Text;
            }
        }
    }
}

Conclusion

In this article we have learned how to pass data between forms using the Properties approach. This can also be done using other methods such as constructor approach, object approach and so on.

Up Next
    Ebook Download
    View all
    Learn
    View all