Option
Explicit
Event
OnClick(
ByVal
Control
As
MSForms.Control)
Private
m_colControls
As
VBA.Collection
Public
Function
AddControl(Container
As
MSForms.UserForm, ProgID
As
String
,
Optional
Name,
Optional
Visible)
As
MSForms.Control
Const
E_NOTIMPL = &H80004001
Dim
ctl
As
Object
Select
Case
UCase$(ProgID)
Case
"FORMS.LABEL.1"
Set
ctl =
New
VBAProject.CtlLabel
Set
ctl.Control = Container.Controls.Add(ProgID, Name, Visible)
Set
ctl.Wrapper =
Me
Case
Else
Err.Raise E_NOTIMPL, TypeName(
Me
) &
"::AddControl()"
,
"invalid or not supported ProgID"
End
Select
Call
m_colControls.Add(ctl)
If
IsMissing(Name)
Then
Name = ctl.Control.Name
If
IsMissing(Visible)
Then
Name = ctl.Control.Visible
Set
AddControl = ctl.Control
End
Function
Public
Sub
Remove(
ByVal
Control
As
Object
)
If
Not
TypeOf
Control
Is
MSForms.Control
Then
Exit
Sub
Dim
i
As
Long
For
i = 1
To
m_colControls.Count
If
m_colControls(i).Control
Is
Control
Then
Call
m_colControls.Remove(i)
Exit
Sub
End
If
Next
Err.Raise 9, TypeName(
Me
) &
"::Remove()"
,
"cannot remove control; not in list"
End
Sub
Public
Function
RemoveAll()
Dim
ctl
As
Object
For
Each
ctl
In
m_colControls
Set
ctl.Control =
Nothing
Set
ctl.Wrapper =
Nothing
Next
Set
m_colControls =
New
VBA.Collection
End
Function
Friend
Sub
InvokeEvent(
ByVal
EventType
As
String
,
ByVal
Caller
As
MSForms.Control)
Select
Case
LCase$(EventType)
Case
"click"
:
RaiseEvent
OnClick(Caller)
End
Select
End
Sub
Private
Sub
Class_Initialize()
Set
m_colControls =
New
VBA.Collection
End
Sub
Private
Sub
Class_Terminate()
Set
m_colControls =
Nothing
End
Sub