首頁 > 後端開發 > C++ > 如何在 C# 中將資料從子窗體傳遞到父窗體?

如何在 C# 中將資料從子窗體傳遞到父窗體?

Susan Sarandon
發布: 2025-01-07 18:46:43
原創
916 人瀏覽過

How to Pass Data from a Child Form to a Parent Form in C#?

在 C# 中從子表單中擷取資料

有效率地將資料從子窗體傳回其父窗體是 C# Windows 窗體開發中常見的任務。 本文示範了一種使用屬性的簡單有效的方法。

當使用 ShowDialog() 開啟的子窗體需要將資料傳回其父窗體時,子窗體上的屬性提供了一個乾淨的解決方案。

這是一個例子:

// 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();
}
登入後複製

此方法在子窗體關閉後使用子窗體上的屬性 (Result) 直接存取資料。 這使得數據傳輸清晰且易於理解。

以上是如何在 C# 中將資料從子窗體傳遞到父窗體?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板