Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to separate custom Close Button from Window_Closing?

I’m building the WPF app with multiple forms, and my goal is that, on, let’s say LoginForm I have a button called Login, which, when clicked, closes current form (LoginForm from above) and opens another form (let’s say Form1).

Note that I’m trying to have only one form opened at a time.

Code –

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

private void btnLogin_Click(object sender, RoutedEventArgs e)
    {
        if(everything okay)
        {
            Form1 frm = new Form1();
            frm.Show();
            Close();
        }
        else
        {
            return;
        }
    }

This works fine. But, my second idea is that, if X in the top right corner is clicked, I want to close this form, and not to open any other, basically exiting the app.

Code-

private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
        MessageBoxResult rez = MessageBox.Show("Do you wish to leave the app?", "Question?", MessageBoxButton.YesNo, MessageBoxImage.Question);

        if (rez != MessageBoxResult.Yes)
        {
            e.Cancel = true;
        }
    }

This is where my problem starts, I’ve found that Window_Closing and this.Close() are same event, so, when I click the Login button, MessageBoxResult still shows up, even tho that is not my goal. I want to somehow separate those two events, if that is possible. How can I do so?

>Solution :

You could temporarily unhook the event handler just before you call Close():

private void btnLogin_Click(object sender, RoutedEventArgs e)
{
    if (everything okay)
    {
        Form1 frm = new Form1();
        frm.Show();
        Closing -= Window_Closing;
        Close();
        Closing += Window_Closing;
    }
    else
    {
        return;
    }
}

Or use a bool variable to determine whether to display the MessageBox:

private bool _showMessageBox = true;

private void btnLogin_Click(object sender, RoutedEventArgs e)
{
    if (everything okay)
    {
        Form1 frm = new Form1();
        frm.Show();
        _showMessageBox = false;
        Close();
        _showMessageBox = true;
    }
    else
    {
        return;
    }
}

private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
    if (!_showMessageBox)
        return;

    MessageBoxResult rez = MessageBox.Show("Do you wish to leave the app?", "Question?", MessageBoxButton.YesNo, MessageBoxImage.Question);

    if (rez != MessageBoxResult.Yes)
    {
        e.Cancel = true;
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading