关于窗口切换与关闭的问题
如上图,在form1点击打开form2,form2点击打开form3,这时,如果我想打开form4时,如何把form3,form2都关闭了?我现在可以在打开form4是把form2关闭了,但是想不到方法把form3也一起关闭,请问有什么方法可以解决??
------解决方案--------------------在Form2的OnClosed事件中关Form3。
------解决方案--------------------1、你在form2里面加个一个公共方法,主要是关闭form3的方法
Dim formshow As New Form3 ---定义写成全局的
Public Sub closefm3()
formshow.Close()
End Sub
2、然后你在form1,中点击form4按钮的事件里面
Dim newform2 As New Form2 ---定义写成全局的
newform2.Close()
newform2.closefm3() ---调用刚才的在form2中定义的方法
----------------------form1代码
Public Class Form1
Dim newform2 As New Form2
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
newform2.Show()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
newform2.Close()
newform2.closefm3()
End Sub
End Class
---------------------form2代码
Public Class Form2
Dim formshow As New Form3
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
formshow.Show()
End Sub
Public Sub closefm3()
formshow.Close()
End Sub
End Class
form3/form4不需要写东西
------解决方案--------------------将子窗口的关闭代码+=到父窗体的Closing中,这样关闭父窗体,自窗体也关闭了。