C# Tips for passing values from subform to parent form
When working with child forms and parent forms in C#, you often need to pass data between them when closing the child form. This article demonstrates how to achieve this seamlessly.
Pass the string value back to the parent form
To pass a string value from a child form (FormOptions) back to the parent form, follow these steps:
Define properties on the subform: On the subform (FormOptions), create a public property to expose the string value to be passed back:
<code class="language-csharp">public string MyResult { get; set; }</code>
Set property value in subform: In the code of the subform, assign the value to the property before closing the form:
<code class="language-csharp">MyResult = "我的返回值"; this.Close();</code>
Retrieve property values in the parent form: In the parent form, instantiate the child form, display it, and retrieve the property value after the child form is closed:
<code class="language-csharp">using (FormOptions formOptions = new FormOptions()) { formOptions.ShowDialog(); string result = formOptions.MyResult; // 使用 result 字符串 }</code>
This approach allows you to easily pass values between child and parent forms, ensuring data is shared during form-based interactions.
The above is the detailed content of How to Pass String Values from Child to Parent Forms in C#?. For more information, please follow other related articles on the PHP Chinese website!