MessageBox 与 YesNoCancel - 否 &取消触发相同的事件

发布于 2024-08-21 13:40:35 字数 379 浏览 5 评论 0原文

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

  • Yes 将执行一些操作并关闭应用程序 - 工作正常
  • No 即可不执行任何操作并关闭应用程序 -(见下文)
  • 取消 将不执行任何操作并保持应用程序打开 -(见下文)。

我使用 DialogResult.No 作为 No 按钮,使用 DialogResult.Cancel 作为 Cancel 按钮。但按下其中任何一个都会触发 DialogResult.Cancel 事件。有什么问题吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

梦醒灬来后我 2024-08-28 13:40:35

这应该可以正常工作:

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

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
玩物 2024-08-28 13:40:35

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

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

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;
        }
尴尬癌患者 2024-08-28 13:40:35

只是为了在 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

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
会傲 2024-08-28 13:40:35
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
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
紫轩蝶泪 2024-08-28 13:40:35

使用:

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

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
殊姿 2024-08-28 13:40:35

这就是在没有 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

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
叹沉浮 2024-08-28 13:40:35

关闭确认警报:

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

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
同展鸳鸯锦 2024-08-28 13:40:35

我使用是/否提示的方式是:

If MsgBox("Are you sure?", MsgBoxStyle.YesNo) <> MsgBoxResults.Yes Then
    Exit Sub
End If

The way I use a yes/no prompt is:

If MsgBox("Are you sure?", MsgBoxStyle.YesNo) <> MsgBoxResults.Yes Then
    Exit Sub
End If
单身狗的梦 2024-08-28 13:40:35

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

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

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
少女净妖师 2024-08-28 13:40:35

试试这个

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

Try this

MsgBox("Are you sure want to Exit", MsgBoxStyle.YesNo, "")
                If True Then
                    End
                End If
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文