Search code examples
c#exitmessagebox

MessageBox when closing form


Upon closing the form by clicking on the button I created named 'Exit' I want it to display a messagebox asking the user "Are you sure you want to exit?" I don't know the syntax for it, can someone help me please? Thanks


Solution

  • if (MessageBox.Show("Are you sure?", "Exit Application?", 
        MessageBoxButtons.YesNo) == DialogResult.No)
    {
        // ignore it
    }
    else
    {
        // shutdown code goes here
    }
    

    There's an MSDN example here.