I would like to ask,
I have a window called MainWindow
and another one called ImportForm
.
In the MainWindow
i call
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.
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)
..
Any help will be apretiated. Thanks
From a child window or a small window once opened, we can transfer any user entered value to main or parent window by using JavaScript. You can see the demo of this here. Here the parent window is known as opener. So the value we enter in a child window we can pass to main by using opener.
A child window is a window that is launched and contained inside of a parent window. By definition, a child window is one that is dependent on a parent window and is minimized or closed when the parent minimizes or closes.
To obtain a window's owner window, instead of using GetParent, use GetWindow with the GW_OWNER flag. To obtain the parent window and not the owner, instead of using GetParent, use GetAncestor with the GA_PARENT flag.
A window that opens inside a parent window is a child window. The action taken on the parent window reflects on the child window as well. For example, if you close the parent window, the child window closes too.
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).
If anything is unclear, let me know.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With