MessageBox Buttons?

6TTW014 picture 6TTW014 · Mar 24, 2011 · Viewed 127.1k times · Source

How would I say if the yes button on the messagebox was pressed do this,that and the other? In C#.

Answer

Lynn Crumbling picture Lynn Crumbling · Mar 24, 2011
  1. Your call to MessageBox.Show needs to pass MessageBoxButtons.YesNo to get the Yes/No buttons instead of the OK button.

  2. 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
}