Home > DeveloperSection > Forums > MessageBox when closing form
Johnny Depp

Total Post:1

Points:7
Posted on    November-11-2013 11:29 PM

 C# C# 
Ratings:


 3 Reply(s)
 782  View(s)
Rate this:
I have a button named "Exit" to exit the application and clicking on it i have to show the message that "Are you sure you want to exit?".

I don't know the syntax for it. 
any help will be appreciated.
Thanks.


ezra heywood
ezra heywood

Total Post:145

Points:1019
Posted on    November-11-2013 11:33 PM

Hi Johnny,

have you tried this :-

private void yourMethodName()
{
if(MessageBox.Show("Are you sure you want to exit?", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
this.Close();
}
else
{
}
}

This will solve your problem.


ezra heywood
ezra heywood

Total Post:145

Points:1019
Posted on    November-11-2013 11:34 PM

Hi Johnny,

have you tried this :-

private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
    {
    if (e.CloseReason == CloseReason.UserClosing)
        {
        dynamic mboxResult = MessageBox.Show("Are you sure you want to exit the form?", "Tranxit CCTV Job Creation Tool", MessageBoxButtons.YesNo);
        if (mboxResult == DialogResult.No)
            {
            /* Cancel the Closing event from closing the form. */
            e.Cancel = true;
            }
 
        else if (mboxResult == DialogResult.Yes)
            {
            /* Closing the form. */
            e.Cancel = false;
            Application.Exit();
            }
        }
    }

This will solve your problem.


Modified On Apr-04-2016 11:13:51 PM

Johnny Depp

Total Post:1

Points:7
Posted on    November-12-2013 1:23 AM

hello

Don't want to miss updates? Please click the below button!

Follow MindStick