Home > other >  Do actions AFTER a window (not the Main Window) is closed in C# WPF
Do actions AFTER a window (not the Main Window) is closed in C# WPF

Time:01-04

In my program, I have a button that opens a new window called CreateNewLabelGroup. This is the click event written in the MainWindow:

private void ButtonCreateNewLabelGroup_Click(object sender, RoutedEventArgs e)
{
   new CreateNewLabelGroup(out write_to_import_textbox).Show();    //Line 1
   ImportedPathTextbox.Text = write_to_import_textbox;            //Line 2
   LoadComboboxItems();                                           //Line 3

}

Line 1 opens the new window.

What I need to happen is that after the user is finshed working in CreateNewLabelGroup, the window will be closed, and only then would Lines 2 and 3 be called. What happens is that Lines 1-3 happen simultaneously.

Line 2 takes a variable created from the CreateNewLabelGroup window and writes it on a textbox in the MainWindow.

Line 3 follows through by loading the array from write_to_import_textbox and lists them in a combobox. LoadComboboxItems() is a method in the MainWindow.

I tried the Closing event (Closing="CreateNewLabelGroup_Closing") but I can only create the event within the CreateNewLabelGroup window.

TIA.

CodePudding user response:

You can attach a close handler outside the window, something like this:

private void ButtonCreateNewLabelGroup_Click(object sender, RoutedEventArgs e)
{
   var labelGroupWindow = new CreateNewLabelGroup(out write_to_import_textbox);
   // Adding a handler for the closing event
   labelGroupWindow.Closing  = LabelGroupWindow_Closing
   // Show Window
   labelGroupWindow.Show()
                                          
}

private void LabelGroupWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e) 
{
   // Your code here
   ImportedPathTextbox.Text = write_to_import_textbox;            
   LoadComboboxItems();

   // Don't forget to delete it after processing the event!
   (sender as Window).Closing -= LabelGroupWindow_Closing;
}

CodePudding user response:

If you need the user to do things in CreateNewLabelGroup before your code continues then you could open the window as a modal dialog. This blocks processing in your main window until the dialog window is closed.

private void ButtonCreateNewLabelGroup_Click(object sender, RoutedEventArgs e)
{
    new CreateNewLabelGroup(out write_to_import_textbox).**ShowDialog**();    //Line 1
    ImportedPathTextbox.Text = write_to_import_textbox;            //Line 2
    LoadComboboxItems();                                           //Line 3

 }

You should really make the calling window "owner" of the dialog as well but that would require some more changes to your code.

See open as modal:

"WPF restricts interaction to the modal window, and the code that opened the window pauses until the window closes. This mechanism provides an easy way for you to prompt the user with data and wait for their response."

https://learn.microsoft.com/en-us/dotnet/desktop/wpf/windows/how-to-open-window-dialog-box?view=netdesktop-6.0

As you can probably see, this is a simpler approach without necessitating subscribing to an event in a separate window.

  • Related