Is there a builtin confirmation dialog in Windows Forms?

C#WinformsDialog

C# Problem Overview


I'd like to create a simple confirm dialog saying "Please check the information and if you're sure it's correct, click OK."

Is there something built in like this?

C# Solutions


Solution 1 - C#

Here is an example. You can try something like this.

var confirmResult =  MessageBox.Show("Are you sure to delete this item ??",
                                     "Confirm Delete!!",
                                     MessageBoxButtons.YesNo);
if (confirmResult == DialogResult.Yes)
{
    // If 'Yes', do something here.
}
else
{
    // If 'No', do something here.
}

You can also try MessageBoxButtons.OKCancel instead of MessageBoxButtons.YesNo. It depends on your requirements.

  1. If you have .Net Framework 4.6 or above please try this.
MessageBoxResult confirmResult = MessageBox.Show("Are you sure to delete this item ??", "Confirm Delete!!", MessageBoxButton.YesNo);`

if (confirmResult == MessageBoxResult.Yes)
{
   // If 'Yes', do something here.
}
else
{
   // If 'No', do something here.
}

Solution 2 - C#

MessageBox.Show? You can specify the title, caption, and a few options for which buttons to display.

On the other hand, if you're asking people to confirm information, that sounds like you probably want to show a custom dialog - which you can do with Form.ShowDialog.

Solution 3 - C#

In .Net Core you can do it like this:

DialogResult dialogResult= MessageBox.Show("Are you sure to delete?", "Confirm", MessageBoxButtons.YesNo);

if (dialogResult == DialogResult.Yes)
{
    //if code here....            
}
else
{
   //else code here.... 
}

Output Result

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondeleteView Question on Stackoverflow
Solution 1 - C#RaaghavView Answer on Stackoverflow
Solution 2 - C#Jon SkeetView Answer on Stackoverflow
Solution 3 - C#Ashutosh MulikView Answer on Stackoverflow