vb.net Visual Basic 程序退出按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46725616/
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
Visual Basic Program Exit Button
提问by E. Bauernschmitt
I am trying to make one of the forms in my Visual Basic (Microsoft Visual Studio 2017) program open another form when someone clicks on the red exit button at the top right of the actual window, but I don't know how to do this. Can anyone please help? Is it possible?
当有人单击实际窗口右上角的红色退出按钮时,我试图让我的 Visual Basic (Microsoft Visual Studio 2017) 程序中的一个表单打开另一个表单,但我不知道如何执行此操作. 有人可以帮忙吗?是否可以?
回答by sapi
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim frm As New Form1
frm.Show()
Me.Close()
End Sub
回答by ebuck
Like Andrew mentioned you are likely looking to utilize the Form.Closing event. This is helpful to fire actions when your program closes.
就像安德鲁提到的那样,您可能希望利用 Form.Closing 事件。这有助于在程序关闭时触发操作。
However this will also exit your program i.e. you're second form will pop up for a split second and then the entire program will close. Therefore you'll likely also then want to use the Form.ShowDialogmethod to bring up the second form. This will prevent your program from exiting until the second form is closed.
但是,这也会退出您的程序,即您的第二个表单将弹出一瞬间,然后整个程序将关闭。因此,您可能还想使用Form.ShowDialog方法来调出第二个表单。这将阻止您的程序退出,直到第二个表单关闭。
See below. Add this sub to your first form.
见下文。将此子添加到您的第一个表单中。
Private Sub Form1_Closing(Sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
Form2.ShowDialog()
End Sub