从子窗口传递到父窗口

本文关键字:窗口 | 更新日期: 2023-09-27 18:18:25

我想问,

我有一个叫做MainWindow的窗口和一个叫做ImportForm的窗口。在MainWindow中,i调用

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

在子窗口我做了一些东西,我产生了一些变量。像var1, var2 var3 .

我想当子窗口关闭返回var1, var2, var3MainWindow并调用一个函数,让我们说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事件中触发,并在主窗口中处理。您还可以将ImportForm显示为一个模态对话框,并在ShowDialog方法返回时读取值。

一个简单的方法是使var1, var2var3实例变量在父类范围内可见(例如使它们为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();
        }
    }