How would I say if the yes button on the messagebox was pressed do this,that and the other? In C#.
Asked
Active
Viewed 1.3e+01k times
6 Answers
68
Your call to
MessageBox.Show
needs to passMessageBoxButtons.YesNo
to get the Yes/No buttons instead of the OK button.Compare the result of that call (which will block execution until the dialog returns) to
DialogResult.Yes
....
if (MessageBox.Show("Are you sure?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
// user clicked yes
}
else
{
// user clicked no
}
![](../../users/profiles/3622940.webp)
Unihedron
- 10,251
- 13
- 53
- 66
![](../../users/profiles/656243.webp)
Lynn Crumbling
- 12,098
- 7
- 56
- 91
-
1@xxMUROxx Please do not edit answers to refactor code. If you feel the need to add additional content with improvements, either leave a comment or add your own answer. – Lynn Crumbling May 11 '15 at 13:15
11
If you actually want Yes and No buttons (and assuming WinForms):
void button_Click(object sender, EventArgs e)
{
var message = "Yes or No?";
var title = "Hey!";
var result = MessageBox.Show(
message, // the message to show
title, // the title for the dialog box
MessageBoxButtons.YesNo, // show two buttons: Yes and No
MessageBoxIcon.Question); // show a question mark icon
// the following can be handled as if/else statements as well
switch (result)
{
case DialogResult.Yes: // Yes button pressed
MessageBox.Show("You pressed Yes!");
break;
case DialogResult.No: // No button pressed
MessageBox.Show("You pressed No!");
break;
default: // Neither Yes nor No pressed (just in case)
MessageBox.Show("What did you press?");
break;
}
}
![](../../users/profiles/390278.webp)
Jeff Mercado
- 113,921
- 25
- 227
- 248
6
if(DialogResult.OK==MessageBox.Show("Do you Agree with me???"))
{
//do stuff if yess
}
else
{
//do stuff if No
}
![](../../users/profiles/479468.webp)
Javed Akram
- 14,220
- 23
- 77
- 113
-
2
-
4This won't work as espected, 'cause the sentences in the else won't be called anytime. – james_bond Mar 24 '11 at 17:24
-
-
2If you're not using one of the `Show` overloads containing the `MessageBoxButtons` parameter, the `MessageBox` will not have any button other than Ok, so the `else` part will never be triggered. Even if you close it via the close box in the top right corner, the result is `DialogResult.OK`. – takrl Apr 18 '16 at 10:30
2
An updated version of the correct answer for .NET 4.5 would be.
if (MessageBox.Show("Are you sure?", "Confirm", MessageBoxImage.Question)
== MessageBoxResult.Yes)
{
// If yes
}
else
{
// If no
}
Additionally, if you wanted to bind the button to a command in a view model you could use the following. This is compatible with MvvmLite:
public RelayCommand ShowPopUpCommand
{
get
{
return _showPopUpCommand ??
(_showPopUpCommand = new RelayCommand(
() =>
{
// Put if statement here
}
}));
}
}
![](../../users/profiles/3841892.webp)
piofusco
- 229
- 2
- 14
0
Check this:
if (
MessageBox.Show(@"Are you Alright?", @"My Message Box",MessageBoxButtons.YesNo) == DialogResult.Yes)
{
//YES ---> Ok IM ALRIGHHT
}
else
{
//NO --->NO IM STUCK
}
Regards
![](../../users/profiles/343540.webp)
Crimsonland
- 2,134
- 3
- 24
- 40
-1
This way to check the condition while pressing 'YES' or 'NO' buttons in MessageBox window.
DialogResult d = MessageBox.Show("Are you sure ?", "Remove Panel", MessageBoxButtons.YesNo);
if (d == DialogResult.Yes)
{
//Contents
}
else if (d == DialogResult.No)
{
//Contents
}
![](../../users/profiles/8158708.webp)
Ismayil S
- 190
- 2
- 18