从子窗口传递到父窗口

5
我想问一下,我有一个名为MainWindow的窗口和另一个名为ImportForm的窗口。在MainWindow中,我调用了以下内容:
private void generate_Window(int num_chart) 
{ 
    Window ownedWindow = new ImportForm(num_chart); 
    ownedWindow.Owner = this;      
    ownedWindow.Show(); 
}

在子窗口中,我进行了一些操作并生成了一些变量,例如var1、var2、var3。我希望当子窗口关闭时,将var1、var2、var3返回到主窗口,并调用一个函数,比如说import_chart(var1, var2, var3)。
非常感谢您的任何帮助。

你可以尝试创建一个事件,并把这些变量放在里面。 - jclozano
4个回答

6

这似乎是一种笨拙的设计选择。无论如何,以下是您可以执行的操作:

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(...)以使用它们即可)。

如果有任何不清楚的地方,请让我知道。


是的,这真的很有帮助...非常感谢...我欠你一个人情...你的回答真的节省了我很多时间。 - Anaisthitos

0
如何在您的ImportForm中添加一个名为“ImportFinished”的事件,其中将您的值作为eventargs传递。该事件在ImportForm的Close或Closing事件中触发,并在MainWindow中处理。 您还可以将ImportForm显示为模态对话框,并在ShowDialog方法返回时读取值。

0
一个简单的方法是将var1var2var3作为实例变量,使其在父级范围内可见(例如,将它们设为public),然后在MainWindow中,附加到Closed事件,并从(ImportForm)sender读取这些变量。

0

我有一个来自我的代码示例。它是通用且明显的,所以我可以在这里分享。

我已经做了类似的事情。

    /// <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();
        }
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接