Click here to Skip to main content
Click here to Skip to main content

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.

Introduction

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.

messages_between_MDI_parent_and_child.png

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)
        {
            frm.Focus();
            return;
        }
    }

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

    child.MessageFromParent = mnuMessageText.Text;
    child.MdiParent = this;
    child.Show();
}

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

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

//override constructor with the parent form as a parameter
public ChildOne(MDI m1_)
{
    InitializeComponent();
    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;
    //this.m1.UpdateMessage(txtMessageToParent.Text);
    this.mdiParent.UpdateMessage();

}

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

Conclusion

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.

License

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

Share

About the Author

pierrecor
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

 
QuestionPassing data between two forms PinmemberManmohan Patil9-Dec-11 21:43 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140821.2 | Last Updated 15 Jan 2009
Article Copyright 2009 by pierrecor
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid