vb.net 多层窗体关闭出现错误的问题
一共3个窗体:Form1,Form2,Form3.
Form1是启动窗体,上边一个按钮Button1,Form2上的按钮Button2,Form3上的窗体Button3.
大致代码如下:(想通过点击Button3关闭整个应用程序)
- VB.NET code
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Form2.ShowDialog()End SubPrivate Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Form3.ShowDialog()End SubPrivate Sub Form2_Closing(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing On Error GoTo showError Form1.Close()showError: MsgBox("Form1:" & ErrorToString(), MsgBoxStyle.Critical, "Form1.Close") ReturnEnd SubPrivate Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.ClickOn Error GoTo showError Me.CloseshowError: MsgBox(ErrorToString(), MsgBoxStyle.Critical, "Form3.Close") ReturnEnd SubPrivate Sub Form3_Closing(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing Form2.Close()End SubPrivate Sub Form1_Closing(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing ‘释放一些在Form1定义的资源End Sub可是Form1的Closing事件触发不了,最终会在Button3的Click事件下的showError中得到错误信息,也就是ErrorToString()的值是:”值不在预期的范围内”.这是在运行到Form1.Close()时返回去的.
这是为什么?我从开始就没有关闭Form1啊?Form1的Closing事件为什么触发不了?
[解决办法]
Button3直接End退出程序即可
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
End
End Sub