Search code examples
c#winformsmessagebox

what is the code for checking if the user press yes\no\cancel ? (c#)


i'm using messageBoxButton.YesNoCancel button, and i wanna know which button the user press

how do i do it ?

this is the code:

 MessageBox.Show("this item is already in the system, do you want to add it anyway ?",
                 "Question",MessageBoxButtons.YesNoCancel);

thanks...


Solution

  • You can store the answers in a DialogResult object. Then you can analyze the answers like that:

           DialogResult result = MessageBox.Show("this item is already in the system, do you want to add it anyway ?", "Question",MessageBoxButtons.YesNoCancel);
    
           if (result == DialogResult.Yes) {
               //your code here
           } else if (result == DialogResult.Cancel) {
               //your code here
           } else if (result == DialogResult.No) {
              // your code here
           }