1,451
社区成员
发帖
与我相关
我的任务
分享
' 假设窗体里面已经有一个控件 MyCtl,Index = 0
Private Sub Form_Load()
Dim i As Long
For i = 1 To 100
Load MyCtl(i)
MyCtl(i).Visible = True
Next
' ...
End Sub
Private Sub MyCtl_Click(Index As Integer)
Debug.Print "MyCtl_Click", Index
End Sub
'-- Class IContainer
Option Explicit
Public Sub GotFocus(Sender As Object)
End Sub
Public Sub LostFocus(Sender As Object)
End Sub
Public Sub ObjectEvent(Sender As Object, Info As EventInfo)
End Sub
'-- Class cDynControl
Option Explicit
Private WithEvents m_oCtl As VBControlExtender
Private m_oContainer As New IContainer
Public Sub Attach(pCtl As Object, pContainer As IContainer)
Set m_oCtl = pCtl
Set m_oContainer = pContainer
End Sub
Public Sub Detach()
Set m_oCtl = Nothing
Set m_oContainer = Nothing
End Sub
Private Sub Class_Terminate()
Detach
End Sub
Private Sub m_oCtl_GotFocus()
m_oContainer.GotFocus m_oCtl
End Sub
Private Sub m_oCtl_LostFocus()
m_oContainer.LostFocus m_oCtl
End Sub
Private Sub m_oCtl_ObjectEvent(Info As EventInfo)
m_oContainer.ObjectEvent m_oCtl, Info
End Sub
-- your form
Implements IContainer
Private m_oControls As New Collection
Private Sub Form_Load()
Dim NewObj As Control
Dim oDynCtl As cDynControl
Set NewObj = Me.Controls.Add("test2.ctl", "f1", Me)
NewObj.Visible = True
NewObj.Height = 5000
NewObj.Width = 5000
Set oDynCtl = New cDynControl
oDynCtl.Attach NewObj, Me
m_oControls.Add oDynCtl
Set NewObj = Me.Controls.Add("test2.ctl", "f2", Me)
NewObj.Visible = True
NewObj.Left = 100
NewObj.Top = 100
NewObj.Height = 5000
NewObj.Width = 5000
Set oDynCtl = New cDynControl
oDynCtl.Attach NewObj, Me
m_oControls.Add oDynCtl
' ...
End Sub
Private Sub IContainer_GotFocus(Sender As Object)
Debug.Print Sender.Name, "GotFocus"
End Sub
Private Sub IContainer_LostFocus(Sender As Object)
Debug.Print Sender.Name, "LostFocus"
End Sub
Private Sub IContainer_ObjectEvent(Sender As Object, Info As EventInfo)
Debug.Print Sender.Name, "ObjectEvent", Info.Name
End Sub