简体   繁体   English

即使按下关闭按钮,程序仍在运行

[英]Program is running even after the close button pressed

I have a 2 form program,the first form's button click will open the second form when clicked. 我有一个2窗体程序,单击第一个窗体的按钮将打开第二个窗体。 I tried using a .close() to hide the first form, but that ended the whole program. 我尝试使用.close()隐藏第一种形式,但这结束了整个程序。 Then I tried using a .hide(). 然后,我尝试使用.hide()。 Now, when I click the X button in the top right of the second form, the program closes, but is still running in the background. 现在,当我单击第二个窗体右上角的X按钮时,该程序关闭,但仍在后台运行。 How do I get around this? 我该如何解决?

By default, a VB.NET application will exit when you close the startup form. 默认情况下,关闭启动表单时,VB.NET应用程序将退出。 If you're hiding the startup form and closing the second form then the startup form is still open, so the app won't exit. 如果您隐藏启动表单并关闭第二个表单,则启动表单仍处于打开状态,因此该应用程序不会退出。

If you want to be able to close the first form without exiting the application then go into the project properties and set the shutdown mode to when all forms close instead of when the startup form closes. 如果您希望能够在不退出应用程序的情况下关闭第一个窗体,请进入项目属性,然后将关闭模式设置为所有窗体关闭时而不是启动窗体关闭时。 That way, you can show your second form and then close your first form. 这样,您可以显示第二个表单,然后关闭第一个表单。 Closing the second form will then exit the app. 关闭第二个表单将退出应用程序。

Simply close the main Form from inside the second form's FormClosed event. 只需从第二个窗体的FormClosed事件内部关闭主Form

Sub Form2_FormClosed(sender As Object, e As FormClosedEventArgs)
    Form1.Close()
End Sub

您应该在第二个窗口的ShowDialog()函数之前使用Me.Close()

if you want the 1st form to be visible again, add a form1.show() to the form_closing event of the 2nd form 如果您希望第一个表单再次可见,则将form1.show()添加到第二个表单的form_closing事件中

if you want the program to end, add an Application.Exit to the form_closing event of the 2nd form 如果您希望程序结束,则将Application.Exit添加到第二个表单的form_closing事件中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM