MessageBox带有YesNoCancel选项 - No和Cancel触发相同的事件。

50

我有一个带有 YesNoCancel 按钮的消息框...

  • 点击 Yes 会执行某些操作并关闭应用程序 - 正常工作
  • 点击 No 什么都不会发生并关闭应用程序 - (见下文)
  • 点击 Cancel 什么都不会发生并保持应用程序打开 - (见下文)。

我在 No 按钮上使用 DialogResult.No ,在 Cancel 按钮上使用 DialogResult.Cancel。但点击它们中的任何一个都会触发 DialogResult.Cancel 事件。问题出在哪里?

10个回答

134

这应该可以正常工作:

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

好的,我明白了我的错误。谢谢。^_^ - Bibhas Debnath

36

我看到所有答案都是正确的。我只想写一小段不同的代码。在我看来,您可以这样做而不使用额外的变量来保存对话框框的结果。看一下:

VB 代码

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# 代码

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;
        }

3

补充一下Darin的例子,下面的代码将显示一个带有方框图标的消息框。 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

使用:

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

通常情况下,如果答案包含代码的意图和解决问题的原因,而不会引入其他问题,那么这样的答案会更有帮助。至少有一个用户标记了此帖子,可能是因为他们认为没有解释的答案应该被删除。在这种情况下,因为问题是在询问“为什么”,而不是“我该如何”,所以他们甚至可能是正确的。 - Nathan Tuggy

3

以下是不使用Dim,而使用MessageBox.Show代替MsgBox的方法。在我看来,这是最为简洁明了的写法!

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

你可以使用If进一步缩短它:
If MessageBox.Show("Message", "Title", MessageBoxButtons.YesNo) = vbYes Then
    ' Other Code goes here
End If

2
我使用yes/no提示的方式是:
If MsgBox("Are you sure?", MsgBoxStyle.YesNo) <> MsgBoxResults.Yes Then
    Exit Sub
End If

2

关闭确认警告:

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

1
我真的不知道这是否是答案,但加上一些评论会让它更清晰明了。 - aliteralmind
1
这个答案只处理消息框上的两种情况(是/否)。OP需要处理三种情况(是/否/取消)。 - Wayne

0

更新奥兰多和彼得的答案。

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

-5

试试这个

MsgBox("Are you sure want to Exit", MsgBoxStyle.YesNo, "")
                If True Then
                    End
                End If

2
那甚至不是有效的VB.NET。 - siride

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接