[英]VB.NET MsgBox Yes only works the second time?
所以这是我的代码。 当我单击表单上的X按钮时,消息框显示并单击“无”,但是当我单击“是”时,消息框将关闭并快速再次出现,然后第二次单击任一按钮将关闭表单。 它出什么问题了?
Private Sub Config_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
Dim result = MessageBox.Show("Would you like to quit?", MessageBoxButtons.YesNo)
If result = DialogResult.No Then
e.Cancel = True
ElseIf result = DialogResult.Yes Then
Application.Exit()
End If
End Sub
提前致谢
Application.Exit
将导致您的表单被关闭(递归),因此您再次看到消息框。 如果用户在消息框中按“是”,您应该只在事件处理程序中执行任何操作,并允许应用程序退出继续。
如果不设置e.Cancel = True
,则表示您希望表单关闭继续。
此代码正常工作
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If (MsgBox("Are you sure to Close", MsgBoxStyle.YesNo, "Close Event") = MsgBoxResult.Yes) Then
e.Cancel = False
Else
e.Cancel = True
End If
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.