I would like to ask,
我想问一下,
I have a window called MainWindow
and another one called ImportForm
. In the MainWindow
i call
我有一个名为MainWindow的窗口和另一个名为ImportForm的窗口。在MainWindow我打电话
private void generate_Window(int num_chart)
{
Window ownedWindow = new ImportForm(num_chart);
ownedWindow.Owner = this;
ownedWindow.Show();
}
In the child window i make some stuff and i produce some variables. like var1,var2,var3.
在子窗口我做了一些东西,我产生了一些变量。像var1,var2,var3。
I want when the child window close to return var1
,var2
,var3
to the MainWindow
and call one function let's say import_chart(var1, var2, var3)
..
我希望当子窗口接近返回var1,var2,var3到MainWindow并调用一个函数时,让我们说import_chart(var1,var2,var3)..
Any help will be apretiated. Thanks
任何帮助都是有益的。谢谢
4 个解决方案
#1
5
It seems like an awkward design choice. Anyways, here is how you can do it:
这似乎是一个尴尬的设计选择。无论如何,这是你如何做到这一点:
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);
}
Just change the Action to the parameter types you need (and adjust ImportForm_Closed(...) to use them as well).
只需将Action更改为您需要的参数类型(并调整ImportForm_Closed(...)以使用它们)。
If anything is unclear, let me know.
如果有什么不清楚,请告诉我。
#2
0
How about adding an event to your ImportForm "ImportFinished" where you pass in your values as the eventargs. This event is fired in the Close or Closing event of the ImportForm and handled in your MainWindow. You could also show the ImportForm as a Modal dialog and read the values when the ShowDialog method returns.
如何将事件添加到ImportForm“ImportFinished”,并将值作为eventargs传递给您。此事件在ImportForm的Close或Closing事件中触发,并在MainWindow中处理。您还可以将ImportForm显示为模态对话框,并在ShowDialog方法返回时读取值。
#3
0
A simple way to do this is to make var1
, var2
, and var3
instance variables that are visible in the scope of the parent (e.g. make them public
), and then in the MainWindow
, attach to the Closed
event, and read the variables from (ImportForm)sender
.
一个简单的方法是创建在父级范围内可见的var1,var2和var3实例变量(例如,将它们公开),然后在MainWindow中,附加到Closed事件,并从中读取变量(ImportForm)发送者。
#4
0
I have an example from my own code. It's generic and obvious enough that I can share it here.
我有一个来自我自己的代码的例子。这是通用的,很明显,我可以在这里分享。
I've done something along these lines.
我已经按照这些方针做了一些事情。
/// <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;
}
I created a class called InputBoxResult like this:
我创建了一个名为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();
}
}
#1
5
It seems like an awkward design choice. Anyways, here is how you can do it:
这似乎是一个尴尬的设计选择。无论如何,这是你如何做到这一点:
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);
}
Just change the Action to the parameter types you need (and adjust ImportForm_Closed(...) to use them as well).
只需将Action更改为您需要的参数类型(并调整ImportForm_Closed(...)以使用它们)。
If anything is unclear, let me know.
如果有什么不清楚,请告诉我。
#2
0
How about adding an event to your ImportForm "ImportFinished" where you pass in your values as the eventargs. This event is fired in the Close or Closing event of the ImportForm and handled in your MainWindow. You could also show the ImportForm as a Modal dialog and read the values when the ShowDialog method returns.
如何将事件添加到ImportForm“ImportFinished”,并将值作为eventargs传递给您。此事件在ImportForm的Close或Closing事件中触发,并在MainWindow中处理。您还可以将ImportForm显示为模态对话框,并在ShowDialog方法返回时读取值。
#3
0
A simple way to do this is to make var1
, var2
, and var3
instance variables that are visible in the scope of the parent (e.g. make them public
), and then in the MainWindow
, attach to the Closed
event, and read the variables from (ImportForm)sender
.
一个简单的方法是创建在父级范围内可见的var1,var2和var3实例变量(例如,将它们公开),然后在MainWindow中,附加到Closed事件,并从中读取变量(ImportForm)发送者。
#4
0
I have an example from my own code. It's generic and obvious enough that I can share it here.
我有一个来自我自己的代码的例子。这是通用的,很明显,我可以在这里分享。
I've done something along these lines.
我已经按照这些方针做了一些事情。
/// <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;
}
I created a class called InputBoxResult like this:
我创建了一个名为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();
}
}