MESSAGEBOX WHEN CLOSING FORM

Johnny Depp

Total Post:1

Points:7
Posted by  Johnny Depp
C# 
C#
 1081  View(s)
Ratings:
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.
  1. ezra heywood

    Post:145

    Points:1019
    Re: MessageBox when closing form

    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.

  1. ezra heywood

    Post:145

    Points:1019
    Re: MessageBox when closing form

    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
  1. Johnny Depp

    Post:1

    Points:7
    Re: MessageBox when closing form

    hello

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!