MessageBox with YesNoCancel - No & Cancel triggers same event
This should work fine:
Dim result As DialogResult = MessageBox.Show("message", "caption", MessageBoxButtons.YesNoCancel)
If result = DialogResult.Cancel Then
MessageBox.Show("Cancel pressed")
ElseIf result = DialogResult.No Then
MessageBox.Show("No pressed")
ElseIf result = DialogResult.Yes Then
MessageBox.Show("Yes pressed")
End If
I see all the answers are correct. I just want to write a little different piece of code. In my opinion, you may do it without using an extra variable to save the result of the dialogBox. Take a look:
VB Code
Select Case MsgBox("Your Message", MsgBoxStyle.YesNoCancel, "caption")
Case MsgBoxResult.Yes
MessageBox.Show("Yes button")
Case MsgBoxResult.Cancel
MessageBox.Show("Cancel button")
Case MsgBoxResult.No
MessageBox.Show("NO button")
End Select
C# Code
switch (MessageBox.Show("Message", "caption", MessageBoxButtons.YesNoCancel))
{
case DialogResult.Yes: MessageBox.Show("Yes"); break;
case DialogResult.No: MessageBox.Show("No"); break;
case DialogResult.Cancel: MessageBox.Show("Cancel"); break;
}
Just to add a bit to Darin's example, the below will show an icon with the boxes. http://msdn.microsoft.com/en-us/library/system.windows.forms.messagebox(v=vs.110).aspx
Dim result = MessageBox.Show("Message To Display", "MessageBox Title", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question)
If result = DialogResult.Cancel Then
MessageBox.Show("Cancel Button Pressed", "MessageBox Title",MessageBoxButtons.OK , MessageBoxIcon.Exclamation)
ElseIf result = DialogResult.No Then
MessageBox.Show("No Button Pressed", "MessageBox Title", MessageBoxButtons.OK, MessageBoxIcon.Error)
ElseIf result = DialogResult.Yes Then
MessageBox.Show("Yes Button Pressed", "MessageBox Title", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If