Private Sub button1_Click(sender As Object, e As System.EventArgs)
Dim f As Form
For Each f In Me.OwnedForms
If TypeOf f Is Form2 Then
'窗体存在则显示
f.Show()
f.Focus()
Return
End If
Next f
'不存在新建窗体显示
Dim f2 As New Form2()
Me.AddOwnedForm(f2)
f2.Owner = Me
f2.Show()
End Sub
'Form2加Owner属性,加form_closing事件处理代码
Public Class Form2
Inherits System.Windows.Forms.Form
Private label1 As System.Windows.Forms.Label
Public Owner As Form
'省略
Private Sub Form2_Closing(sender As Object, e As System.ComponentModel.CancelEventArgs)
Owner.RemoveOwnedForm(Me)
End Sub 'Form2_Closing
End Class
---------------模块内容-------------------
Module Module1
Public Frm2Show As Boolean
End Module
------------------------------------------
---------------FORM1内容-------------------
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Frm2Show Then
Form2.Activate()
Else
Form2.Show()
End If
End Sub
End Class
------------------------------------------
---------------FORM2内容-------------------
Public Class Form2
Private Sub Form2_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
Frm2Show = False
End Sub
Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Frm2Show = True
End Sub
End Class