不用动态加载变量,只是希望一个窗体重复使用三次,通过方法dim frm as new from1 得到的三个窗体 要怎么区分,我看了下name 都是一样的,name属性没法设置。 因为三个窗体不一定会同时使用,有可能是使用一个,两个,三个。或者中途会关闭其中一个或者多个 所以我要去检索当前有哪些是在使用中的,好给新增加的窗体分配哪个 new得到的 没有在使用的窗体
可以给new出来的新窗体重新设置caption来进行区别,也可以利用tag
假定 Form1 是调用者,Form2 是需要多实例的被调用者。 1)比较正式的做法是给 Form2 创建一个用于区分实例的属性,偷懒一点可以用公共变量代替。 'Form2 Option ExplicitPrivate m_ID As StringPublic Property Get ID() As String ID = m_ID End PropertyPublic Property Let ID(ByVal RHS As String) m_ID = RHS End PropertyPrivate Sub Form_Load() Me.Caption = "ID = " & m_ID & ", hWnd = &H" & Hex(Me.hWnd) End Sub 2)这样就可以遍历 Forms 判断指定标识的实例是否创建 'Form1 Option ExplicitPrivate Function FindOrCreateByID(ByVal ID As String) As Form2 Dim frm As Form Dim frmTarget As Form2
For Each frm In Forms If TypeOf frm Is Form2 Then Set frmTarget = frm If frmTarget.ID = ID Then Exit For Else Set frmTarget = Nothing End If End If Next
If frmTarget Is Nothing Then Set frmTarget = New Form2 frmTarget.ID = ID End If
Set FindOrCreateByID = frmTarget End FunctionPrivate Sub Command1_Click() FindOrCreateByID("One").Show End SubPrivate Sub Command2_Click() FindOrCreateByID("Two").Show End SubPrivate Sub Command3_Click() FindOrCreateByID("Three").Show End Sub
Option ExplicitPrivate Sub Command1_Click()
Dim frmB As Form
Set frmB = New Form1
frmB.Show
End Sub
就可以了,太罗嗦了
通过什么来判断 ?
否则你只能动态加载控件,但你能动态加载变量吗?(利用tag除外)。
要怎么区分,我看了下name 都是一样的,name属性没法设置。
因为三个窗体不一定会同时使用,有可能是使用一个,两个,三个。或者中途会关闭其中一个或者多个
所以我要去检索当前有哪些是在使用中的,好给新增加的窗体分配哪个 new得到的 没有在使用的窗体
1)比较正式的做法是给 Form2 创建一个用于区分实例的属性,偷懒一点可以用公共变量代替。
'Form2
Option ExplicitPrivate m_ID As StringPublic Property Get ID() As String
ID = m_ID
End PropertyPublic Property Let ID(ByVal RHS As String)
m_ID = RHS
End PropertyPrivate Sub Form_Load()
Me.Caption = "ID = " & m_ID & ", hWnd = &H" & Hex(Me.hWnd)
End Sub
2)这样就可以遍历 Forms 判断指定标识的实例是否创建
'Form1
Option ExplicitPrivate Function FindOrCreateByID(ByVal ID As String) As Form2
Dim frm As Form
Dim frmTarget As Form2
For Each frm In Forms
If TypeOf frm Is Form2 Then
Set frmTarget = frm
If frmTarget.ID = ID Then
Exit For
Else
Set frmTarget = Nothing
End If
End If
Next
If frmTarget Is Nothing Then
Set frmTarget = New Form2
frmTarget.ID = ID
End If
Set FindOrCreateByID = frmTarget
End FunctionPrivate Sub Command1_Click()
FindOrCreateByID("One").Show
End SubPrivate Sub Command2_Click()
FindOrCreateByID("Two").Show
End SubPrivate Sub Command3_Click()
FindOrCreateByID("Three").Show
End Sub