Bibhas Bibhas - 6 months ago 32
Vb.net Question

MessageBox with YesNoCancel - No & Cancel triggers same event

I have a message box with the

YesNoCancel
buttons...


  • Pressing
    Yes
    will do some action and close the application - works fine

  • Pressing
    No
    will do nothing and close the application - (see below)

  • Pressing
    Cancel
    will do nothing and keep the application open - (see below).



I'm using
DialogResult.No
for the No button and
DialogResult.Cancel
for the Cancel button. But pressing either of them triggers
DialogResult.Cancel
event. What's the problem?

Answer

This should work fine:

Dim result As Integer = 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
Comments