子窗題用個 shared 變量boolean來保存是否已經被實例化
構造函數設為私有
用shared成員方法返回新的實例
中間判斷設置的shared bool
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()
a 是任何控件包括窗体
Msgbox(Not (a Is Nothing OrElse a.IsDisposed))
True是实例化
上面实例通常是定义Form为全局变量,就不用判断是否已实例化
对VB.net感兴趣的请加入15636854讨论群
[此贴子已经被作者于2006-10-10 21:06:17编辑过]