vb.net 从另一个 Winform 上的按钮关闭 Winform。表格不会关闭
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17934725/
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
Closing winform from button on another winform. Form will not close
提问by Jose M.
I have 2 forms on my application. I have the main form that opens TopMost, CenterScreen and Maximized. Then I have another form on this screen that pops open when I press a button. That second screen has a button that navigates to another screen, so when I press that button the second form closes, and the main form is suppose to close as well and the selected sheet open up.
我的申请中有 2 个表格。我有打开 TopMost、CenterScreen 和 Maximized 的主窗体。然后我在这个屏幕上有另一个表单,当我按下一个按钮时它会弹出。第二个屏幕有一个导航到另一个屏幕的按钮,所以当我按下那个按钮时,第二个窗体关闭,主窗体也应该关闭,选定的工作表打开。
However, the second screen closes fine, but my main screen remains open and active, while the called sheet opens but does not enable. I track down what was happening and the issue is that form all code runs, but the main screen does not seem to want to close. Here is my code:
但是,第二个屏幕关闭正常,但我的主屏幕保持打开和活动状态,而被调用的工作表打开但未启用。我追踪了发生的事情,问题是所有代码都在运行,但主屏幕似乎不想关闭。这是我的代码:
Private Sub btnOpenDashboard_Click(sender As Object, e As EventArgs) Handles btnOpenDashboard.Click
Dim welcomeForm As New frmWelcomePage
If lblReportTitle.Text = "Employee Dashboard" Then
Me.Close() 'This works
welcomeForm.Close() 'This one remains open and active
Globals.dsbEmployeeBoard.Select() 'This one opens but is not enabled
End If
End Sub
回答by DeborahK
I assume from your description that you already have a welcome form created and displayed before the form with the button is displayed.
我假设根据您的描述,在显示带有按钮的表单之前,您已经创建并显示了一个欢迎表单。
This line of code: "Dim welcomeForm As New frmWelcomePage"
这行代码:“DimwelcomeForm As New frmWelcomePage”
is creating a newcopy of the Welcome Page and closing it.
正在创建欢迎页面的新副本并关闭它。
Instead of creating a new one, you need to reference the original one that is open.
您需要引用打开的原始文件,而不是创建一个新文件。
If I recall correctly, you should be able to just remove that line and use frmWelcomPage.Close.
如果我没记错的话,您应该能够删除该行并使用 frmWelcomPage.Close。
回答by Karl Anderson
You need to pass a reference of your first form (Form1) to the second form (Form2), so that in the second form you can close the first form, like this:
您需要将第一个表单 ( Form1)的引用传递给第二个表单 ( Form2),以便在第二个表单中您可以关闭第一个表单,如下所示:
Public Class Form2 Inherits Form
Private _form1 As Form1
Public Sub New(form1 As Form1)
Me.Form1 = form1
End Sub
End Class
Private Sub btnOpenDashboard_Click(sender As Object, e As EventArgs) Handles btnOpenDashboard.Click
If lblReportTitle.Text = "Employee Dashboard" Then
_form1.Close()
End If
End Sub
Then when you instantiate Form2, you would pass a reference to Form1, like this:
然后,当您实例化 时Form2,您将传递一个对 的引用Form1,如下所示:
Dim form2 As New Form2(Me)
Note: Meis the instance of Form1.
注:Me是Form1.

