Jesse
Jesse

Reputation: 484

Closing WPF program from another class

I am working with a class that is inheriting my main window class. I am attempting to close out my Window, but it seems to not be working and provides no error. Maybe I do not understand the way this inheritance works, but please help me understand.

I am only inheriting my MainWindow class to try to gain access to my close method. If this isn't the best approach, please let me know.

Main Window Class:

public partial class MainWindow : Window
 {

    public MainWindow()
    {
        InitializeComponent();
    }

    protected void CloseProgram()
    {
        Close();
    }

    private void Go_Button_Click(object sender, RoutedEventArgs e)
    {
        ExcelOperations temp = new ExcelOperations();
        temp.Begin();
    }
}

From my other class:

class ExcelOperations : MainWindow
{

    private void MethodName()
    {
        try
        {
            // My Code Here
        }
        catch
        {
            // Show Error Message
            // I have tried this.Close(); but it does nothing
            this.CloseProgram(); // This also seems to do nothing
        }
    }
}

Upvotes: 0

Views: 486

Answers (3)

Grant Winney
Grant Winney

Reputation: 66439

There's multiple ways you could do this, but in general I wouldn't put a class in charge of ending your application.

You could still handle the error in the class (to display an error), but then throw the exception up the chain, so the click event can close the form.

private void MethodName()
{
    try
    {
        // My Code Here
    }
    catch
    {
        // Show Error Message
        throw;
    }
}

Back in your main window:

private void Go_Button_Click(object sender, RoutedEventArgs e)
{
    try
    {
        ExcelOperations temp = new ExcelOperations();
        temp.Begin();
    }
    catch
    {
        // close the program
    }
}

Edit:

Servy provided an excellent explanation of why you're having an issue.

It's because ExcelOperations is never shown. He thinks that by inheriting the main window he's able to manipulate it, simply because he has access to the appropriate methods, but it's actually manipulating (i.e., closing) a second window that was never shown in the first place.

So ExcelOperations shouldn't inherit from MainWindow at all (I know you were just doing that to try and close from the class), and that particular logic should remain within MainWindow itself.

Upvotes: 2

user153923
user153923

Reputation:

Pass your MainWindow class to your 2nd ExcelOperations class ...and do not inherit from MainWindow:

class ExcelOperations
{
    private MainWindow parent;

    public ExcelOperations(MainWindow parent)
    {
      this.parent = parent;
    }

    private void MethodName()
    {
        try
        {
            // My Code Here
        }
        catch
        {
            // Show Error Message
            parent.Close();
        }
    }
}

Upvotes: 0

Allan Elder
Allan Elder

Reputation: 4094

You have two main windows. The MainWindow that is being created when you start the app and the ExcelOperations window that you are creating when the Go_Button_Click event is fired.

If you want to explicitly shutdown the application, the Application.Shutdown() method will do that for you.

You can also control how the application shuts down via the ShutdownMode

Upvotes: 0

Related Questions