Jonathan
Jonathan

Reputation: 599

Closing Windows in WinForms

I have two buttons on my WinForm in C# (File-Close & the Red X). I'd like both of them to behave the same way, so I have created a helper method that is called upon when the two different events are fired. Here's what I have so far.

private void CloseFileOperation(object e)
{
    // If the user selected the exit buttton from the main title bar,
    // then handle the closing event properly.
    if (e is FormClosingEventArgs)
    {
        FormClosingEventArgs FormCloser = (FormClosingEventArgs)e;

        // If the Spreadsheet has been changed since the user opened it and
        // the user has requested to Close the window, then prompt them 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();
                    FormCloser.Cancel = false;
                    break;
                case DialogResult.No:
                    FormCloser.Cancel = false;
                    break;
                case DialogResult.Cancel:
                    FormCloser.Cancel = true;
                    return;
            }
        }

        // If the Spreadsheet hasn't been changed since the user opened it, then
        // simply Close the window.
        else
            FormCloser.Cancel = false;
    }

    // Otherwise the user must have selected the "Close" option from the File menu.
    // Handle the event in the following manner.
    else
    {
        // 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 call on this function when the user selects the Red X & File-Close options. Notice that when the user selects File-Close the appropriate action calls on the Close() operation, which in turn calls on the closing event. As a result of this the warning dialog "Would you like to save your changes" is displayed twice.

Is there an easy way to do this similar to how I have it currently that will eliminate the window being displayed twice?

Upvotes: 2

Views: 714

Answers (2)

Sudhakar Tillapudi
Sudhakar Tillapudi

Reputation: 26209

1.If you want to handle Form Closing Event Wherever it is (either from RED X Button or File->Close Button) Handle The Form_Closing Event so that it is the only place invoked for all closing events.

2.if user Says YES ->Save the FileOperation and leave it as it is so the form is closed.

Note : no need to Close the form using-> this.Close()

3.if user Says NO -> leave it as it is so the form is closed.

Note : no need to Close the form using-> this.Close()

4.if user Says Cancel-> here form should not be closed so change the FormClosingEventArgs parameter 'e' property Cancel to true so closing operation will be cancelled and form willbe remain open.

 private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                    // 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();
                                break;
                            case DialogResult.No:
                                break;
                            case DialogResult.Cancel:
                                e.Cancel = true;
                                break;                              
                        }
                    }
            }

Upvotes: 2

ddavison
ddavison

Reputation: 29062

Instead of your File->Close executing this.Close() i'd use the event handler that you are using for the Red X.

For example ->

public void FileCloseHandler() {
    RedXHandler() // instead of this.Close()
}

Upvotes: 1

Related Questions