MessageBox 与 YesNoCancel - 否 &取消触发相同的事件
我有一个带有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
这应该可以正常工作:
This should work fine:
我看到所有答案都是正确的。我只是想写一些不同的代码。在我看来,您可以不使用额外的变量来保存对话框的结果。看一下:
VB代码
C#代码
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
C# Code
只是为了在 Darin 的示例中添加一些内容,下面将显示一个带有框的图标。
http://msdn。 microsoft.com/en-us/library/system.windows.forms.messagebox(v=vs.110).aspx
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
使用:
Use:
这就是在没有
Dim
的情况下,使用MessageBox.Show
而不是MsgBox
来完成此操作的方法。我认为这是最干净的书写方式!您可以使用
If
进一步缩短它:This is how you can do it without a
Dim
, usingMessageBox.Show
instead ofMsgBox
. This is in my opinion the cleanest way of writing it!You can shorten it down even further by using
If
:关闭确认警报:
Closing conformation alert:
我使用是/否提示的方式是:
The way I use a yes/no prompt is:
更新奥兰多和彼得的答案。
Update answer of Orlando and Peter.
试试这个
Try this