Search code examples
c#buttonmessagebox

MessageBox Buttons?


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


Solution

    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
    }