MythrilMagician
MythrilMagician

Reputation: 23

Creating program in C#, FormClosing event executes twice

As I said in the title I am creating a program. However, I am facing the problem of the FormClosing event executing twice. The message box appears and the buttons perform their purpose well, but when I click "Yes" or "No", it repeats itself. Thankfully the "Cancel" button doesn't have this problem.

private void Form1_FormClosing (object sender, FormClosingEventArgs e)
{
    DialogResult dialog = MessageBox.Show("Do you want to save your progress?", "Media Cataloguer", MessageBoxButtons.YesNoCancel);

    if (dialog == DialogResult.Yes)
    {
        SaveFileDialog savefile = new SaveFileDialog();
        savefile.Filter = "Text files|*.txt";
        savefile.Title = "Save As";
        savefile.ShowDialog();
        System.IO.FileStream fs = (System.IO.FileStream)savefile.OpenFile();
        Application.Exit();
    }
    else if (dialog == DialogResult.No)
    {
        MessageBox.Show("Are you sure?", "Media Cataloguer", MessageBoxButtons.YesNo);
        Application.Exit();
    }
    else if (dialog == DialogResult.Cancel)
    {
        e.Cancel = true;
    }
}

Nothing else I have found had helped me very much. Like I said earlier, the message box appears twice. That is my only problem. Everything else for this void works fine.

Upvotes: 0

Views: 968

Answers (3)

DrewJordan
DrewJordan

Reputation: 5314

You really shouldn't be throwing more than a single messagebox at the user... you might want to read this, this, and this. That said, I consider exiting with a prompt to save as one of the good spots for one, but not two.

You've already got the correct mechanism in place (they can answer Yes, No, or Cancel). Revise your question to be more clear to the user: "Would you like to save your work before exiting?" If they cancel, then cancel like you are using e.Cancel. Otherwise, just let the form close on it's own.

If they answer no, don't ask again. I can hear them now...

"I already told you, just EXIT!!!"

Upvotes: 0

Noel Widmer
Noel Widmer

Reputation: 4572

Your second MessageBox does not make sense and you do not have to exit the application.
The window should close if you do not set e.Cancel to true:
https://msdn.microsoft.com/en-us/library/system.windows.window.closing%28v=vs.110%29.aspx

private void Form1_FormClosing (object sender, FormClosingEventArgs e) {
    DialogResult dialog = MessageBox.Show("Do you want to save your progress?", "Media Cataloguer", MessageBoxButtons.YesNoCancel);

    if (dialog == DialogResult.Yes) {
        SaveFileDialog savefile = new SaveFileDialog();
        savefile.Filter = "Text files|*.txt";
        savefile.Title = "Save As";
        savefile.ShowDialog();
        System.IO.FileStream fs = (System.IO.FileStream)savefile.OpenFile();
    } else if (dialog == DialogResult.No) {
        if(MessageBox.Show("Are you sure?", "Media Cataloguer", MessageBoxButtons.YesNo) == DialogResult.No){
           e.Cancel = true;
        }
    } else if (dialog == DialogResult.Cancel) {
        e.Cancel = true;
    }
}

I would not quit the app in a window closing event.
It is not designed to perform that task.
You could use the project settings to define when the application quits.
Or if you need more control than that, you may want to handle it in the App.cs.
But I wouldn't do it in here.

Upvotes: 1

David Arno
David Arno

Reputation: 43254

Your problem is that you are calling Application.Exit(). As MSDN says,

The Exit method stops all running message loops on all threads and closes all windows of the application

In other words, it will fire the form closing event again.

To get around this, use Environment.Exit(0) instead.

Upvotes: 4

Related Questions