Retrieving Data from Child Forms in C#
Efficiently transferring data from a child form back to its parent is a common task in C# Windows Forms development. This article demonstrates a simple and effective method using properties.
When a child form, opened with ShowDialog()
, needs to return data to its parent, a property on the child form provides a clean solution.
Here's an example:
<code class="language-csharp">// In the parent form: using (FormOptions formOptions = new FormOptions()) { formOptions.ShowDialog(); string result = formOptions.Result; // Access the data through the property // Process the 'result' data... } // In the child form (FormOptions): public string Result { get; set; } // Property to hold the data private void button1_Click(object sender, EventArgs e) { Result = textBox1.Text; // Set the property value before closing this.Close(); }</code>
This approach directly accesses the data using a property (Result
) on the child form after the child form is closed. This keeps the data transfer clear and easy to understand.
The above is the detailed content of How to Pass Data from a Child Form to a Parent Form in C#?. For more information, please follow other related articles on the PHP Chinese website!