Click here to Skip to main content
13,351,457 members (46,330 online)
Click here to Skip to main content
Add your own
alternative version


34 bookmarked
Posted 15 Jan 2009

Pass data back to the calling form

, 15 Jan 2009
Rate this:
Please Sign up or sign in to vote.
This article describes how to pass data from the parent to the child form and back.


Sometimes, you have to return data to a calling form (parent) based on an action or data change on the called form (child). There are various methods to pass data from the parent to the child on opening the child form. The not so obvious way is to use the parent form as a parameter in the child form’s overridden constructor. This will then expose all the public properties and methods of the parent form in the child form, which, of course, can be populated or invoked from the child form.

This article use an MDI scenario with one child form to pass data from the MDI parent form to the child form on opening the child form. Once the child form is open, data can be passed back to the MDI parent form by invoking a method in the child form.


Using the code

  1. Create an MDI form and a child form as in the image, with a File | Open menu click event, and a TextBox on the menu bar for the message to be sent to the child form when opening the child form. Also, add a status bar to display the message passed back by the child form.
  2. Create a normal Windows Form which will be the child form, with a Label to display the message from the calling MDI parent, and a TextBox to enter the message to be passed back to the MDI parent as well as a Button to invoke the method to pass the message back to the MDI parent.

MDI parent form

The MDI parent form has:

  • A Click event to open the child form
  • A property to hold the data returned by the child form
  • Two methods - one overriding - how to display the message passed back from the child form

Opening the child form from the MDI parent

First, enter some text in the textbox on the menu bar of the MDI parent form and use File | Open to open the child form.

The mnuOpen_Click event first checks to see if the child form is already open, and sets focus to it if so. A child form is instantiated by calling the overridden constructor of the child form using the MDI parent form as the parameter. This is done to have an instantiated MDI parent in the child form once open to call the public properties and methods of the MDI parent from the child form.

The text message is passed from the MDI parent to the MessageFromParent property in the child form for use by the child form once open. Since the child form is already instantiated, you can access all public methods and properties of the child form.

The rest of the code is the normal way of opening a child form.

private void mnuOpen_Click(object sender, EventArgs e)
    //if child form is already open set focus to the form
    foreach (Form frm in this.MdiChildren)
        if (frm is ChildOne)

    //call the overrided constructor of the child form
    ChildOne child = new ChildOne(this);

    child.MessageFromParent = mnuMessageText.Text;
    child.MdiParent = this;

Display message from the MDI parent

In the Load event of the child form, call the DisplayMessageFromParent method to display the value of the MessageFromParent property.

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

private void ChildOne_Load(object sender, EventArgs e)

public void DisplayMessageFromParent()
    lblMessageFromParent.Text = MessageFromParent;

Pass message back to the MDI parent

As mentioned, the child form has an overriding constructor to accept the MDI parent form as a parameter. This allows the child form to access all public properties and methods of the MDI parent form.

private MDI mdiParent;
public ChildOne()

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

Enter some text in the text box on the child form and hit the button.

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


I have added two methods to send the message back to the MDI parent. I prefer to use properties (hence the commented out lines), but there is nothing wrong by calling a method direct with a parameter.

In the MDI parent, it is simply a matter of displaying the property value in the status bar or wherever you prefer.

//property to recieve a message from a child form
private string childMessage;
public string ChildMessage
    get { return childMessage; }
    set { childMessage = value; }

public void UpdateMessage()
    lblStatusMessage.Text = ChildMessage;

public void UpdateMessage(string message)
    lblStatusMessage.Text = message;


There are many other approaches to pass data between forms, but to transfer my meager knowledge, I prefer to keep it simple and straightforward. I have not added code to pass data to the child once open; I’ll leave it to you to find the way. Read more in the article “Passing Data Between Forms” by Thiagarajan Alagarsamy in The Code Project about other methods to pass data between forms.


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


About the Author

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.

You may also be interested in...


Comments and Discussions

Suggestionworking with two mdi parent forms Pin
Mayank Topiwala10-Sep-11 0:49
memberMayank Topiwala10-Sep-11 0:49 
GeneralRe: working with two mdi parent forms Pin
pierrecor10-Sep-11 4:04
memberpierrecor10-Sep-11 4:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180111.1 | Last Updated 15 Jan 2009
Article Copyright 2009 by pierrecor
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid