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.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3