您总是在创建 的新实例Form2
,但您应该真正关闭现有实例,并且只有在您想打开新表单时才创建新实例。
' We are keeping our opened form reference here!
Dim openForm As Form2
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
If Application.OpenForms.OfType(Of Form2).Any() Then
' No `openForm = new Form2` here - we need to close the existing instance
' and not to create new one
openForm.Close()
Else
' Create new instance only here!
openForm = New Form2()
openForm.Show()
End If
或者
Dim openForms = Application.OpenForms.OfType(Of Form2)()
' If there is Form2 instance opened
If openForms.Any() Then
' Get it and close it!
openForms.First().Close()
Else
Dim openForm As New Form2
openForm.Show()
End If