2

I have a C# GUI application. When the user clicks on the red 'X' (for closing the app) I want to show a message and ask if he really wants to close it.

I found a solution:

        DialogResult dialog = MessageBox.Show("Do you really want to close the program?", "SomeTitle", MessageBoxButtons.YesNo);
        if (dialog == DialogResult.Yes)
        {
            Application.Exit();
        }else if (dialog == DialogResult.No)
        {
          //don't do anything
        }

When the user clicks 'yes', the application should terminate completely. (Is Application.Exit() correct for this purpose?)

When the user clicks 'no', the DialogResult/MessageBox should close, but the application should stay opened. However, it closes!!

How can I avoid this?

BTW: I use Visual Studio 2010 and Winforms.

John Arlen
  • 6,539
  • 2
  • 33
  • 42
Nicky
  • 93
  • 1
  • 4
  • 8
  • 4
    Don't do this; it's really annoying. – SLaks May 08 '12 at 19:11
  • 1
    I always hope for a "Yes, dammit!" button on such a dialog so it will never ask me such a silly thing again, but haven't seen one yet. Set the this.DialogResult property back to None to prevent it closing. – Hans Passant May 08 '12 at 19:12
  • sorry but this isn't very helpful. it is a requirement to implement this, so thats why i am asking;) – Nicky May 08 '12 at 19:17

4 Answers4

5

Use the FormClosing event from the Form, and the FormClosingEventArgs to cancel the process.

example:

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        DialogResult dialog = dialog = MessageBox.Show("Do you really want to close the program?", "SomeTitle", MessageBoxButtons.YesNo);
        if (dialog == DialogResult.No)
        {
            e.Cancel = true;
        }
    }
Gustavo F
  • 2,071
  • 13
  • 23
  • Ok, I should expect 3 almost equal responses :(. – Gustavo F May 08 '12 at 19:16
  • the e is the formcloseingeventargs. e.cancel means cancel the formclosing, therefor it will reamin open. – General Grey May 08 '12 at 19:22
  • I am use the this code to my first form. It's working correctly. again I used this code to my 2nd form in that application. but It's working not correctly. two times comes confirmation box exit the application. why that? – TipVisor Apr 06 '20 at 15:32
2

Use the form's FormClosing event of your program window. You can then set e.Cancel to true if the user clicks no:

this.FormClosing += (s, e) => {
  DialogResult dialog = dialog = MessageBox.Show("Really close?", "SomeTitle",
    MessageBoxButtons.YesNo);
  if (dialog == DialogResult.No)
  {
    e.Cancel = true;
  }
};

I guess you are using FormClosed. Are you? Then it's too late.

Matthias Meid
  • 12,455
  • 7
  • 45
  • 79
0

Try this

        this.FormClosing += new FormClosingEventHandler(delegate(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("Do you really want to exit this application?", MessageBoxButtons:=MessageBoxButtons.YesNo) == DialogResult.No)
            {
                e.Cancel = true;
            }
        });
Rasmus Søborg
  • 3,597
  • 4
  • 29
  • 46
0

Refer to Mudu's answer.

Basically unless you specify additional parameters to MessageBox.Show() you cannot get any result other than DialogResult.Ok from a default MessageBox.

The code you posted (minus your little typo of dialog = dialog =) works exactly as expected to me.

Also: Application.Exit() IS the correct way of closing an application :)

Nathan Dortman
  • 406
  • 1
  • 7
  • 20