簡體   English   中英

從子窗口傳遞到父窗口

[英]Pass from child window to parent window

我想問一下,

我有一個叫做MainWindow的窗口,另一個叫ImportForm MainWindow我打電話

private void generate_Window(int num_chart) 
{ 
    Window ownedWindow = new ImportForm(num_chart); 
    ownedWindow.Owner = this;      
    ownedWindow.Show(); 
}

在子窗口中,我做一些東西,並產生一些變量。 像var1,var2,var3。

我想要在子窗口關閉時將var1var2var3返回到MainWindow並調用一個函數,例如import_chart(var1, var2, var3)

任何幫助將被取消。 謝謝

似乎是一個尷尬的設計選擇。 無論如何,這是您可以執行的操作:

MainWindow.cs:

private void generate_Window(int num_chart)
{
    Window ownedWindow = new ImportForm(num_chart, import_chart); 
    ownedWindow.Owner = this; 
    ownedWindow.Show();
}

private void import_chart(int n, string s, bool b)
{
    //Do something
}

ImportForm.cs:

private Action<int, string, bool> callback;
public ImportForm(int num_chart, Action<int, string, bool> action)
{
    InitializeComponent();
    Closed += ImportForm_Closed;
    callback = action;
}

private void ImportForm_Closed(object sender, EventArgs e)
{
    callback(0, "Test", false);
}

只需將Action更改為所需的參數類型(並調整ImportForm_Closed(...)即可使用它們)。

如果有任何不清楚的地方,請通知我。

如何將事件添加到ImportForm“ ImportFinished”中,在此您將值作為eventargs傳遞。 此事件在ImportForm的Close或Closing事件中觸發,並在您的MainWindow中處理。 您還可以將ImportForm顯示為模態對話框,並在ShowDialog方法返回時讀取值。

一種簡單的方法是使var1var2var3實例變量在父級范圍內可見(例如,將它們設為public ),然后在MainWindow ,附加到Closed事件,並從中讀取變量(ImportForm)sender

我有一個自己的代碼示例。 它很通用,很明顯,我可以在這里分享。

我已經按照這些原則做了一些事情。

    /// <summary>
    /// Show an InputBox similar to the pre-.NET InputBox functionality. Returns the original value when Cancel was pressed.
    /// </summary>
    /// <param name="OriginalValue">Pre-populated value in the input box</param>
    /// <param name="PromptText">Prompt text displayed on the form</param>
    /// <param name="Caption">Window caption</param>
    /// <returns>InputBoxResult structure containing both the DialogResult and the input text. Warning: The input text will always be returned regardless of the DialogResult, so don't use it if the DialogResult is Cancel.</returns>
    public static InputBoxResult Show(string OriginalValue = "", string PromptText = "", string Caption = "") {
        InputBox form = new InputBox {
           Text = Caption,
            lblPrompt = {Text = PromptText}, 
            txtInput = {Text = OriginalValue}
        };

        InputBoxResult res = new InputBoxResult();
        res.Result = form.ShowDialog();
        res.Input = form.txtInput.Text;

        return res;
    }

我創建了一個名為InputBoxResult的類,如下所示:

    /// <summary>
    /// Represents the results from an InputBox.Show call, including the DialogResult
    /// and the input data. Note that the input data is always populated with the
    /// user-entered data regardless of the DialogResult - this is inconsistent with
    /// the old InputBox behavior and may change in the future.
    /// </summary>
    public struct InputBoxResult {
        /// <summary>
        /// Describes the way the dialog was resolved (OK / Cancel)
        /// </summary>
        public DialogResult Result { get; set; }
        /// <summary>
        /// User-entered text
        /// </summary>
        public string Input { get; set; }

        /// <summary>
        /// Translate this result into a string for easy debugging
        /// </summary>
        /// <returns></returns>
        public override string ToString() {
            return "Result: " + Result.ToString() +
                "\r\nInput: " + Input.ToString();
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM