2

My program has two ways to close, one being the 'X' in the top-right corner and the other being an 'Exit' button. Now, when either one of these is pressed when a certain condition is met, a message pops up notifying the user that they haven't saved yet. If they DID save, the message won't pop up and the program closes as normal. Now, when the message DOES pop up, the user gets a MessageBox with Yes and No buttons. If 'Yes' is pressed, the program needs to save. If 'No' is pressed, the program needs to cancel the close event that has been initiated when the user pressed the 'X' or 'Exit' button.

What is the best way to do this?

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    TryClose();
}

private void TryClose()
{
    if (saved == false)
    {
        //You forgot to save
        //Turn back to program and cancel closing event
    }
}
Annoying Bot
  • 359
  • 1
  • 5
  • 13

7 Answers7

4

FormClosingEventArgs includes a Cancel property. Just set e.Cancel = true; to prevent the form from closing.

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    if (!saved)
        e.Cancel = true;
}

Edit in response to comments:

Since your goal is to allow the same "save method" to be used, I would change it to return bool on success:

private bool SaveData()
{
     // return true if data is saved...
}

Then you can write:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    // Cancel if we can't save
    e.Cancel = !this.SaveData();
}

And your button handlers, etc, can all still call SaveData() as needed.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • The point is that the save feature is being embedded from a method, and that method doesn't have the 'FormClosingEventArgs e' property. More buttons or events use the save feature. – Annoying Bot Mar 01 '13 at 23:19
1

This will do what you need:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    e.Cancel = !TryClose();
}

private bool TryClose()
{
    return DialogResult.Yes == MessageBox.Show("Are you sure?", "Are you sure?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
}
Adam Plocher
  • 13,994
  • 6
  • 46
  • 79
0

Use the Cancel property of the event args, set it to true to cancel.

Grant Thomas
  • 44,454
  • 10
  • 85
  • 129
0

To cancel the closing event just set the Cancel property to true on the FormClosingEventArgs instance

if (!saved) {
  // Message box
  e.Cancel = true;
}
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
0

You can call Close from the exit button. and then Handle closing in the Forms.FormClosing event as others have said. This will handle both Exit Button Click and Forms Closing from "X"

Amit
  • 191
  • 4
  • 14
0

Override OnFormClosing:

 protected override void OnFormClosing(FormClosingEventArgs e)
 {
    if (saved == true)
    {
       Environment.Exit(0);
    }
    else /* consider checking CloseReason: if (e.CloseReason != CloseReason.ApplicationExitCall) */
    {
       //You forgot to save
       e.Cancel = true;
    }
    base.OnFormClosing(e);
 }
BlueMonkMN
  • 25,079
  • 9
  • 80
  • 146
0
    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        e.Cancel = !TryClose();
    }

    private bool TryClose()
    {
        if (!saved)
        {
            if (usersaidyes)
            {
                // save stuff
                return true;
            }
            else if (usersaidno)
            {
                // exit without saving
                return false;
            }
            else
            {
                // user cancelled closing
                return true;
            }
        }
        return true;
    }
Tim
  • 857
  • 6
  • 13