Hot File

Pass data back to the calling form

View: 663    Dowload: 0   Comment: 0   Post by: naruto  
Author: none   Category: Form - Instructions   Fields: Computers - Technology

0 point/3 review File has been tested

Download   pass-data-back-to-the-calling.zip (26.24 KB)

You need to Sign In to download the file pass-data-back-to-the-calling.zip
If you do not have an account then Sign up for free here

Download error   Report copyright violation

Is your email address OK? You are signed up for our newsletters but your email address is either unconfirmed, or has not been reconfirmed in a long time. Please click here to have a confirmation email sent so we can confirm your email address and start sending you newsletters again. Alternatively, you can update your subscriptions.

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.

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  by Thiagarajan Alagarsamy in The Code Project about other methods to pass data between forms.

Pass data back to the calling form

Pass data back to the calling form Posted on 21-03-2014  Is your email address OK? You are signed up for our newsletters but your email address is either unconfirmed, or has not been reconfirmed in a long time. Please click here to have a confirmation email sent so we can confirm your email address and start sending you newsletters again. Alternatively, you can update your subscriptions. 5/10 663

Comment:

To comment you must be logged in members.

Files with category

  • Form Management in MOSS 2007

    View: 2864    Download: 0   Comment: 0   Author: none  

    Form Management in MOSS 2007

    Category: Form - Instructions
    Fields: Other

    0/7 review
    The purpose of this document is to give an overview of Form Management in Microsoft Office SharePoint Server 2007. This article explains how to create and publish forms using Microsoft Office InfoPath 2007 in to SharePoint site.

  • Mac OS X Theme for Windows Forms

    View: 3348    Download: 2   Comment: 0   Author: none  

    Mac OS X Theme for Windows Forms

    Category: Form - Instructions
    Fields: Other

    0/9 review
    Some programmers might want to apply some themes or skins to their forms so that it will look cool, sophisticated, and attractive. I have created a Mac OS X open source theme library for everyone and the usage of it is indicated on this article.

  • Those Crazy Popups

    View: 1792    Download: 0   Comment: 0   Author: none  

    Those Crazy Popups

    Category: Form - Instructions
    Fields: Other

    0/1 review
    Ever tried to create a pop-up in ASP.NET? Well, in this article, we will see different ways to create them. We'll also see a few issues surrounding the different ways.

  • Handling .NET forms and their events in VB6 main application

    View: 3373    Download: 1   Comment: 0   Author: none  

    Handling .NET forms and their events in VB6 main application

    Category: Form - Instructions
    Fields: Other

    0/6 review
    I always wanted to post an article in CodeProject. I was always "taking" not "giving" anything back to this community and that made me feel guilty. I think its time to "give" back something useful to this community

  • Beginners’ Guide To Web Design

    View: 489    Download: 0   Comment: 0   Author: none  

    Beginners’ Guide To Web Design

    Category: Form - Instructions
    Fields: Other

    0/2 review
    If you’re a total newbie to the whole web designing business, you’ve come to the right place. Web design is a tricky thing to deal with, and with that being said there are many things that you have to keep in mind. Enumerated below are the things...

  • GraphicRiver.net – A Review

    View: 682    Download: 0   Comment: 0   Author: none  

    GraphicRiver.net – A Review

    Category: Form - Instructions
    Fields: none

    0/1 review
    Designers are always looking for resources to maximize their revenue. Whether it is a useful freelancing website, a blog or a marketplace, they never fail to entertain a designer’s desire to earn and learn, too. In this article I am going to review...

  • How to Establish a Dreamweaver Website on Localhost – EasyPhp

    View: 1025    Download: 0   Comment: 0   Author: none  

    How to Establish a Dreamweaver Website on Localhost – EasyPhp

    Category: Form - Instructions
    Fields: none

    0/4 review
    While developing a website, it is sometimes quite annoying and repetitive to edit the code in the editor and then test it on the browser, especially when working with WordPress. So, to ensure smooth workflow and faster development, we use a feature...

  • Top 15 Websites to learn Web Design and Development

    View: 720    Download: 0   Comment: 0   Author: none  

    Top 15 Websites to learn Web Design and Development

    Category: Form - Instructions
    Fields: none

    0/1 review
    People are, at all times looking to enhance their skills at Designing and Developing websites. Formal education in this field is quite rare so in this article, I’m going to share the 15 Top Websites to Learn Web Design and Development from, more...

 
Newsletter Email

File suggestion for you

File top downloads

logo codetitle
Codetitle.com - library source code to share, download the file to the community
Copyright © 2015. All rights reserved. codetitle.com Develope by Vinagon .Ltd