Click here to Skip to main content
15,881,172 members
Articles / Desktop Programming / Windows Forms

Pass data back to the calling form

Rate me:
Please Sign up or sign in to vote.
4.63/5 (7 votes)
15 Jan 2009CPOL3 min read 83.7K   2.9K   36  
This article describes how to pass data from the parent to the child form and back.
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 MDI
{
    public partial class ChildOne : Form
    {
        //This project is to show to pass data between forms after the forms are open.
        //I used the typical MDI parent and child scenarion where somthing is happening in the
        //child form and you need to display a message or invoke a method in the
        //parent form.

        //Read more about passing data between forms in the 
        //article "Passing Data Between Forms" by Thiagarajan Alagarsamy in The Code Project"
        //http://www.codeproject.com/KB/cs/pass_data_between_forms.aspx

        private MDI mdiParent;

        #region Properties...

        private string messageFromParent;
        public string MessageFromParent 
		{
			get { return messageFromParent; }
            set { messageFromParent = value; }
		}

        #endregion


        #region Initialize...

        public ChildOne()
        {
            InitializeComponent();
        }

        //override constructor with the parent form as a parameter
        public ChildOne(MDI m1_)
        {
            InitializeComponent();
            this.mdiParent = m1_;
        }

        //call the method to display the value send by the 
        //parent form to the child's property
        private void ChildOne_Load(object sender, EventArgs e)
        {
            DisplayMessageFromParent();

        }

        #endregion


        #region Methods and functions...

        //this step is not really necassary, but I like to use properties to store the data.
        //You could have in the ChildOne_Load event used:
        //lblMessageFromParent.Text = MessageFromParent;
        //directly
        public void DisplayMessageFromParent()
        {
            lblMessageFromParent.Text = MessageFromParent;

        }

        #endregion


        #region Click Events...

        private void btnPassMessageToParent_Click(object sender, EventArgs e)
        {
            this.mdiParent.ChildMessage = txtMessageToParent.Text;
            //this.m1.UpdateMessage(txtMessageToParent.Text);
            this.mdiParent.UpdateMessage();


        }

        #endregion

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Work for mining company
South Africa South Africa
Live in the best part of South Africa - the West Coast. Many moons in IT - more into the architect of bringing systems together than the final coding of an application - reckon myself as between a beginner and just under an intermediatee in C#. An expert Googelite, but has no knowledge of Web development - yet.

Comments and Discussions