MessageBox with YesNoCancel - No & Cancel triggers same event

前端 未结 9 1106
误落风尘
误落风尘 2020-12-04 21:23

I have a message box with the YesNoCancel buttons...

  • Pressing Yes will do some action and close the application - works fine
9条回答
  •  爱一瞬间的悲伤
    2020-12-04 21:43

    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
    

提交回复
热议问题