vb.net 带有 YesNoCancel 的 MessageBox - No & Cancel 触发相同的事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2256909/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MessageBox with YesNoCancel - No & Cancel triggers same event
提问by Bibhas Debnath
I have a message box with the YesNoCancel
buttons...
我有一个带有YesNoCancel
按钮的消息框...
- 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).
- 按
Yes
将执行一些操作并关闭应用程序 - 工作正常 - 按
No
将什么都不做并关闭应用程序 - (见下文) - 按
Cancel
将什么也不做,并保持应用程序打开 - (见下文)。
I'm using DialogResult.No
for the Nobutton and DialogResult.Cancel
for the Cancelbutton. But pressing either of them triggers DialogResult.Cancel
event. What's the problem?
我使用DialogResult.No
的No按钮,并DialogResult.Cancel
为Cancel按钮。但是按下它们中的任何一个都会触发DialogResult.Cancel
事件。有什么问题?
回答by Darin Dimitrov
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
回答by Orlando Herrera
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:
我看到所有的答案都是正确的。我只想写一点不同的代码。在我看来,您可以不使用额外的变量来保存 dialogBox 的结果。看一看:
VB Code
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# Code
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;
}
回答by yousafkamal
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
回答by David Kittell
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
只是在 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
回答by Prashant Koli
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
回答by Suji
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
回答by DLS Discovery
The way I use a yes/no prompt is:
我使用是/否提示的方式是:
If MsgBox("Are you sure?", MsgBoxStyle.YesNo) <> MsgBoxResults.Yes Then
Exit Sub
End If
回答by Elias Wick
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!
这就是你可以在没有 的情况下做到这一点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
You can shorten it down even further by using If
:
您可以使用If
以下命令进一步缩短它:
If MessageBox.Show("Message", "Title", MessageBoxButtons.YesNo) = vbYes Then
' Other Code goes here
End If
回答by Tanmay Nehete
Try this
尝试这个
MsgBox("Are you sure want to Exit", MsgBoxStyle.YesNo, "")
If True Then
End
End If