Home > Backend Development > C++ > How to Pass String Values from Child to Parent Forms in C#?

How to Pass String Values from Child to Parent Forms in C#?

Patricia Arquette
Release: 2025-01-07 19:07:41
Original
516 people have browsed it

How to Pass String Values from Child to Parent Forms in C#?

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:

  1. 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>
    Copy after login
  2. 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>
    Copy after login
  3. 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>
    Copy after login

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!

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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template