Tuesday, July 23, 2013

Create Yes/No/Cancel buttons in MessageBox Windows Application

This code snippets will elaborate handling MessageBox Yes/No/Cancel button in Windows Application using C#. Follow below steps to create a small application to do this by yourself else download the attachment for the sample application with visual studio 2010.

1.) Create a Windows Application and give it a preferred name.
2.) Add a form to the solution and add a Button as displayed below image.

3.) Now double click the button and add following code snippet as displayed below.
private void btnSave_Click(object sender, EventArgs e)
{
  // get the result 
  DialogResult result = MessageBox.Show("Hi, do you want to test messagebox buttons?",
  "MessageBox Buttons Demo", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
 if (result == DialogResult.Yes)
 {
    // if user clicked 'Yes' button
 }
 else if (result == DialogResult.No)
 {
   // if user clicked 'No' button
 }
 else
 {
   // if user clicked 'Cancel' button
 }
}

4.) Now press the "Save" button which shows below messagebox. You can changed the MesageBoxIcon either MessageBoxIcon.Exclamation,MessageBoxIcon.Asterisk.


5.) Based on the result Yes/No or Cancel modify your code to manage your application.

Download

2 comments:

  1. In a windows based application when a messagebox has yes no cancel buttons, it is easier for the users to interact with the software. Such instances are also beneficial when users are required to confirm activities or are presented with options. Just like in custom web application development at o16labs, the presence of distinct actions boosts the usability of the application and guarantees optimum selection.

    ReplyDelete