子窗題用個 shared 變量boolean來保存是否已經被實例化
Private Function GetInstanceState(ByVal name As String) As Boolean
'获得frmMain窗体的子窗体的数量
Dim i As Integer = Me.MdiChildren.Length
'循环判断是否有名为name的子窗体实例
For i = 0 To Me.MdiChildren.Length - 1
If Me.MdiChildren(i).Name = name Then
'存在名为name的子窗体,是子窗体获得焦点并返回True
Me.MdiChildren(i).Focus()
Return True
End If
Next
'不存在名为Name的子窗体False
Return False
End Function
............
If GetInstanceState("logmonitor") Then
Exit Sub
End If
Dim frm As New logmonitor
frm.MdiParent = Me
frm.Show()