Sunday 5 January 2014

Passing the values between the windows forms - C#

In this article we are going to see , how to pass the value between the forms.First let we see how to pass the value from the child screen to the parent form,next parent form to child form. For transfer the data we have to use the Property.

Child form to Parent Form:
In this sample we are going to launch student form, in which the textbox is readonly we cant able to enter the value, the value should be calculate from the child form then it should fill the textbox in the parent form.

Parent Form:
public partial class ParentForm : Form
    {
        public ParentForm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ChildForm cf = new ChildForm();
            cf.ShowDialog();
            textBox1.Text = cf.TotalVaue.ToString();
        }
    }



Child Form:

public partial class ChildForm : Form
    {
        public int TotalVaue { set; get; }

        private int a;

        private int b;

        public ChildForm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox1.Text) || string.IsNullOrEmpty(textBox2.Text))
            {
                MessageBox.Show(":Please fill the details to calculate the operation ");
            }
            else
            {
                try
                {
                    a = int.Parse(textBox1.Text);
                    b = int.Parse(textBox2.Text);
                    TotalVaue = a + b;
                    this.Close();
                }
                catch
                {
                    MessageBox.Show(":Please fill the details to calculate the operation ");
                }
            }
        }
    }



Output:






Parent Form to Child Form:
In this second sample we are going to pass the value to child form from parent form using property. So here in this sample Student name is pass from the parent form to child form.


Parent Form:

if (string.IsNullOrEmpty(textBox2.Text))
            {
                MessageBox.Show("Please fill the student name");
            }
            else
            {
                ChildForm cf = new ChildForm();
                cf.StudentName = textBox2.Text;
                cf.ShowDialog();
                textBox1.Text = cf.TotalVaue.ToString();
            }


Child Form :
private void ChildForm_Load(object sender, EventArgs e)
        {
            label4.Text = StudentName;
        }



Output:










I hope from this article you can learn how to pass the data from the parent form to the child form.



2 comments:

  1. It 's an amazing article and useful for developers
    Dot Net Online Training

    ReplyDelete
  2. Google Workspace formerly G Suite, is a suite of cloud-based productivity tools designed to enable collaboration and communication within organizations.

    ReplyDelete