Question

I am trying to create an event handler such that when the user selects the close (x) button, it provides a prompt to the user to save any unsaved changes. Here is my C# code:

private void CloseFileOperation()
{
    // If the Spreadsheet has been changed since the user opened it and
    // the user has requested to Close the window, then prompt him to Save
    // the unsaved changes.
    if (SpreadSheet.Changed)
    {
        DialogResult UserChoice = MessageBox.Show("Would you like to save your changes?", "Spreadsheet Utility",
                MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);

        switch (UserChoice)
        {
            case DialogResult.Yes:
                SaveFileOperation();
                this.Close();
                break;
            case DialogResult.No:
                this.Close();
                break;
            case DialogResult.Cancel:
                return;
        }
    }

    // If the Spreadsheet hasn't been changed since the user opened it, then
    // simply Close the window.
    else
        this.Close();
}

I created and event handler MainFram_FormClosing that is triggered when the user selects the close (x) button.

private void MainFrame_FormClosing(object sender, FormClosingEventArgs e)
{
    // Close the Spreadsheet.
    CloseFileOperation();
}

Whenever I select the close button the application crashes though.. I have read the replies from this post. I guess I am violating the Windows 7 Program Requirements. I guess I don't understand why this functionality can't be accomplished this easily.

What's the best approach at this?

Was it helpful?

Solution

If you use the event handler itself instead of a separate routine, you have access to the FormClosingEventArgs which will allow you to cancel the closing if necessary. Also you're using this.Close();, which just restarts the event, instead of returning and letting the event finish. When I use this code set as the event handler it works on Win7 as expected:

    private void Form2_FormClosing(object sender, FormClosingEventArgs e)
    {
        if(SpreadSheet.Changed)
        {
            switch(MessageBox.Show("Would you like to save your changes?", "Spreadsheet Utility",
                MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning))
            {
                case DialogResult.Yes:
                    SaveFileOperation();
                    return;
                case DialogResult.No:
                    return;
                case DialogResult.Cancel:
                    e.Cancel = true;
                    return;
            }
        }
    }

OTHER TIPS

My Answer is:

1.Don't call this.close() function as you are already in closing event.

2.if you want to change the closing event action(close or don't close) you can simply set the FormClosingEventArgs parameter (e in below code) property cancel to true to cancel closing or false to close.

3.if sheet is not saved you dont need to do anything , in that case form should be closed simply without prompting. hence you can ignore the else block.

here the modified code is:

if (SpreadSheet.Changed)

            {
                DialogResult UserChoice = MessageBox.Show("Would you like to save your changes?", "Spreadsheet Utility",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Warning);

                switch (UserChoice)
                {
                    case DialogResult.Yes:
                       SaveFileOperation();
                        break;
                case DialogResult.No:
                    break;
                case DialogResult.Cancel:
                    e.Cancel = true;
                    break;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top