VB.NET:中止 FormClosing()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2256869/
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
VB.NET: Abort FormClosing()
提问by Bibhas Debnath
I have a code snippet that I want to run when the app is closing. So, I used FormCLosing
event. But now i wanna place a confirmation message for exiting. Like, if the user clicks the Exit(X
) button, there'll be a prompt, if he clicks NO, then the app will not close and revert to previous state.
我有一个代码片段,我想在应用程序关闭时运行它。所以,我使用了FormCLosing
事件。但现在我想放置一个退出确认消息。就像,如果用户点击 Exit( X
) 按钮,就会有一个提示,如果他点击 NO,那么应用程序不会关闭并恢复到以前的状态。
Now I find that hard to achieve using FormClosing
event. because it'll get executed no matter what button the user clicks.
Any remedy for that?
现在我发现使用FormClosing
事件很难实现。因为无论用户点击什么按钮它都会被执行。对此有什么补救措施吗?
I mean, I need an even like ExitButtonPressed()
..
我的意思是,我需要一个甚至喜欢ExitButtonPressed()
..
回答by Adriaan Stander
You could try something like
你可以尝试类似的东西
Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
If (MessageBox.Show("Close?", "", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.No) Then
e.Cancel = True
End If
End Sub
Have a look at
看一下
And
和
CancelEventArgs.Cancel Property
The event can be canceled by setting the Cancel property to true.
可以通过将 Cancel 属性设置为 true 来取消该事件。
回答by Ali Ozger
'Button2 and closebutton of the form both closes the form asking the same 'question
'Button2 和 closebutton 的窗体都关闭窗体询问相同的 ' 问题
Dim button2Yes As Boolean = False Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim button2Yes As Boolean = False Private Sub Button2_Click(sender As Object, e As EventArgs) 处理 Button2.Click
If MessageBox.Show(" Sure to close? ", "CLOSING CONTROL", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
button2Yes = True
Me.Close()
Else
button2Yes = False
End If
End Sub
Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Me.FormClosing
If Not button2Yes Then
If Not MessageBox.Show(" Sure to close? ", "CLOSING CONTROL", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
e.Cancel = True
End If
End If
End Sub