none
how to respond to messageBox buttons in c# RRS feed

  • Question

  • How do I respond to a MessageBox button press? I know that just using messageBoxButtons.OK will give me one OK button, and that will cloase the message box. I want to put in MessageBoxButtons.OKCANCEL and have OK close the message box and go ahead wih whatever the program was trying to do (in my case clear the text from a richTextBox) and if the user clicks Cancel, to just close the messageBox and not clear the text (I am making a text editor where I want a "Do you want to save first? like in Word when you quit or create a new document without saving).
    Sunday, December 13, 2009 2:45 PM

Answers

  • Hi,

    Here is a sample.

    Hope it helps.

                //you can also use OcCancel enumeration instead of YesNo, then you should check DialogResult.OK
                DialogResult dr = MessageBox.Show("Do you want To Save?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dr == DialogResult.Yes)
                {
                    //clear the richtextbox
                }
                else
                {
                    //do whatever you want
                }
    • Marked as answer by Acegard Sunday, December 13, 2009 2:56 PM
    Sunday, December 13, 2009 2:53 PM

All replies

  • Hi,

    Here is a sample.

    Hope it helps.

                //you can also use OcCancel enumeration instead of YesNo, then you should check DialogResult.OK
                DialogResult dr = MessageBox.Show("Do you want To Save?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dr == DialogResult.Yes)
                {
                    //clear the richtextbox
                }
                else
                {
                    //do whatever you want
                }
    • Marked as answer by Acegard Sunday, December 13, 2009 2:56 PM
    Sunday, December 13, 2009 2:53 PM
  • Thanks man. I just plugged it in and it works perfectly for my purposes.

    Sunday, December 13, 2009 2:56 PM