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:
public string ReturnValue1 { get; set; } public string ReturnValue2 { get; set; }
private void btnOk_Click(object sender, EventArgs e) { this.ReturnValue1 = "Something"; this.ReturnValue2 = DateTime.Now.ToString(); //example this.DialogResult = DialogResult.OK; this.Close(); }
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; } }
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!