如何在OK按钮上退出程序?

时间:2014-01-05 04:44:26

标签: vb.net winforms exit

我有一个名为exit的按钮,它调用一个msg框,要求用vbOKCancel进行确认。当用户按下OK时,如何让程序退出?

这就是我目前的情况:

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

谢谢!

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

理想情况下,您可能希望避免粗暴地退出您的应用程序,但End会做到这一点。

我的意思是你可以在你的按钮点击事件的处理程序中调用End,但更好的方法是尝试优雅地关闭你正在使用vbscript的任何应用程序(即如果它运行的是VB脚本)在Access数据库应用程序内部,您可能希望在主Access应用程序对象上调用Close方法。

答案 2 :(得分:0)

Application.exit确实是正确的,但如果您使用红色X关闭表单,也可以使用e.cancel = false关闭表单。

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