50

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Bibhas Debnath
  • 14,559
  • 17
  • 68
  • 96

10 Answers10

134

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
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
36

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;
        }
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Orlando Herrera
  • 3,481
  • 1
  • 34
  • 44
3

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
3
dim result as dialogresult
result = 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
3

Use:

Dim n As String = MsgBox("Do you really want to exit?", MsgBoxStyle.YesNo, "Confirmation Dialog Box")
If n = vbYes Then
    MsgBox("Current Form is closed....")
    Me.Close() 'Current Form Closed
    Yogi_Cottex.Show() 'Form Name.show()
End If
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • Generally, answers are much more helpful if they include an explanation of what the code is intended to do, and why that solves the problem without introducing others. (This post was flagged by at least one user, presumably because they thought an answer without explanation should be deleted. In this case, because the question is asking *why* not *how do I*, they might even be right.) – Nathan Tuggy May 30 '15 at 01:11
3

This is how you can do it without a Dim, using MessageBox.Show instead of MsgBox. This is in my opinion the cleanest way of writing it!

Select Case MessageBox.Show("Message", "Title", MessageBoxButtons.YesNo)
    Case vbYes
        ' Other Code goes here
    Case vbNo
        ' Other Code goes here
End Select

You can shorten it down even further by using If:

If MessageBox.Show("Message", "Title", MessageBoxButtons.YesNo) = vbYes Then
    ' Other Code goes here
End If
Elias Wick
  • 483
  • 6
  • 21
2

Closing conformation alert:

Private Sub cmd_exit_click()

    ' By clicking on the button the MsgBox will appear
    If MsgBox("Are you sure want to exit now?", MsgBoxStyle.YesNo, "closing warning") = MsgBoxResult.Yes Then ' If you select yes in the MsgBox then it will close the window
               Me.Close() ' Close the window
    Else
        ' Will not close the application
    End If
End Sub
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Suji
  • 1,326
  • 1
  • 12
  • 27
  • 1
    I honestly don't know if this is the answer or not, but it would be some commentary would make it a lot clearer. – aliteralmind Jul 08 '14 at 13:16
  • 1
    This answer only handles two conditions (Yes/No) on the message box. The OP needs to handle three conditions(Yes/No/Cancel). – Wayne Sep 29 '16 at 15:29
2

The way I use a yes/no prompt is:

If MsgBox("Are you sure?", MsgBoxStyle.YesNo) <> MsgBoxResults.Yes Then
    Exit Sub
End If
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

Update answer of Orlando and Peter.

Select Case MsgBox("Your Message", VbMsgBoxStyle.vbYesNoCancel, "caption")
                    Case VbMsgBoxResult.vbYes
                        MsgBox "Yes button"
                    Case VbMsgBoxResult.vbCancel
                        MsgBox "Cancel button"
                    Case VbMsgBoxResult.vbNo
                        MsgBox "No button"
End Select
Manrecky
  • 11
  • 1
-5

Try this

MsgBox("Are you sure want to Exit", MsgBoxStyle.YesNo, "")
                If True Then
                    End
                End If
Tanmay Nehete
  • 2,138
  • 4
  • 31
  • 42