Home > Backend Development > C++ > How to Efficiently Return Values from a C# Child Form to its Parent MDI Form?

How to Efficiently Return Values from a C# Child Form to its Parent MDI Form?

DDD
Release: 2024-12-26 06:42:13
Original
173 people have browsed it

How to Efficiently Return Values from a C# Child Form to its Parent MDI Form?

Returning Values from Forms in C#

In a scenario where a child form (frmHireQuote) is opened from a parent MDI form (frmMainMDI) using ShowDialog(), how can we efficiently pass values from the child form back to specific text boxes on the parent form, while ensuring that the values are returned to the correct parent instance?

Solution

To return values from the child form (frmImportContact) to the parent form (frmHireQuote), follow these steps:

  1. Create Public Properties in the Child Form: Define public properties in the child form to store the values to be returned. For example:
public string ReturnValue1 { get; set; }
public string ReturnValue2 { get; set; }
Copy after login
  1. Set Properties in the Child Form: When the user clicks the OK button on the child form, set the public properties with the desired values. For example:
private void btnOk_Click(object sender, EventArgs e)
{
    this.ReturnValue1 = "Something";
    this.ReturnValue2 = DateTime.Now.ToString(); //example
    this.DialogResult = DialogResult.OK;
    this.Close();
}
Copy after login
  1. Retrieve Properties in the Parent Form: In the parent form (frmHireQuote), when opening the child form, use a using block to capture the return result:
using (var form = new frmImportContact())
{
    var result = form.ShowDialog();
    if (result == DialogResult.OK)
    {
        string val = form.ReturnValue1; //values preserved after close
        string dateString = form.ReturnValue2;
        //Do something here with these values

        //for example
        this.txtSomething.Text = val;
    }
}
Copy after login

By following these steps, you can effectively return values from a child form to specific text boxes on the parent form, ensuring that the values are retrieved from the correct instance of the parent form.

The above is the detailed content of How to Efficiently Return Values from a C# Child Form to its Parent MDI Form?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template