vb.net 如何在“确定”按钮上退出我的程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20930158/
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
How do I exit my program on OK button?
提问by user3161856
I have a button called exit that calls up a msg box asking for confirmation with vbOKCancel. How do I get the program to exit when the user presses OK?
我有一个名为 exit 的按钮,它调用一个 msg 框,要求使用 vbOKCancel 进行确认。当用户按下 OK 时,如何让程序退出?
This is what I have currently:
这是我目前所拥有的:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles EditGoals.Click
GoalCountdown.Text = "Congratulations"
End Sub
Private Sub ExitProgram_Click(sender As Object, e As EventArgs) Handles ExitProgram.Click
If MsgBox("Are you sure you want to exit?", MsgBoxStyle.OkCancel) = MsgBoxResult.Ok Then
Exit Sub
End If
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
End Sub
End Class
Thanks!
谢谢!
回答by Andrew Morton
Use Application.Exit()for a .NET Windows Forms application:
将Application.Exit()用于 .NET Windows 窗体应用程序:
Private Sub ExitProgram_Click(sender As Object, e As EventArgs) Handles ExitProgram.Click
If MsgBox("Are you sure you want to exit?", MsgBoxStyle.OkCancel) = MsgBoxResult.Ok Then
Application.Exit()
End If
End Sub
回答by Mike Dinescu
Ideally you would want to avoid brutally exiting your application but End
will do the trick.
理想情况下,您希望避免粗暴地退出您的应用程序,但End
会做到这一点。
By that I mean that you could call End
in the handler of your button's click event but a better approach would be to try to gracefully close whatever application you are using vbscript with (ie. If it's VB Script running inside of an Access database app then you would want to invoke the Close method on the main Access application object).
我的意思是,您可以调用End
按钮单击事件的处理程序,但更好的方法是尝试正常关闭正在使用 vbscript 的任何应用程序(即,如果它是在 Access 数据库应用程序中运行的 VB 脚本,那么您想调用主 Access 应用程序对象上的 Close 方法)。
回答by Vahx
Application.exit
is indeed correct, but you can also use e.cancel = false
to close a form if you are closing it with the red X.
Application.exit
确实是正确的,但是e.cancel = false
如果您使用红色 X 关闭表单,您也可以使用它来关闭表单。
Private Sub Form1Close(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
Dim Result As DialogResult
If Result = Windows.Forms.DialogResult.OK Then
e.Cancel = False
ElseIf Result = Windows.Forms.DialogResult.Cancel Then
e.Cancel = True
End If
End Sub
--
——
Private Sub Form1Close(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
Dim Result As DialogResult
If Result = Windows.Forms.DialogResult.OK Then
Application.Exit()
ElseIf Result = Windows.Forms.DialogResult.Cancel Then
e.Cancel = True
End If
End Sub