Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass from child window to parent window

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

like image 604
Anaisthitos Avatar asked Dec 21 '11 12:12

Anaisthitos


People also ask

How do I pass a value from child window to parent window?

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.

What does a child window mean?

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.

How do I get a parent window?

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.

What is a child window of two level window?

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.


1 Answers

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.

like image 84
Adam Avatar answered Oct 15 '22 18:10

Adam