'**** ComboBox Class - called clsComboBox
Private WithEvents objComboBox As VB.ComboBox
Private objParent As colComboboxes
'Access to the combobox on a form
Public Property Get ComboBox() As VB.ComboBox
Set ComboBox = objComboBox
End Property
Public Property Set ComboBox(ComboBoxControl As VB.ComboBox)
Set objComboBox = ComboBoxControl
End Property
'Although this is creates a circular reference it shouldn't be a
'problem if handled correctly. Make sure everything is terminated
'properly
Public Property Set Parent(ComboBoxes As colComboboxes)
Set objParent = ComboBoxes
End Property
'Tell the collection class to raise the events for this combobox
'to its form
Private Sub objComboBox_Change()
objParent.RaiseEvents Me, "Change"
End Sub
Private Sub objComboBox_Click()
objParent.RaiseEvents Me, "Click"
End Sub
Private Sub Class_Terminate()
Set objCmboBox = Nothing
Set objParent = Nothing
End Sub
'**** Collection class - called colComboBoxes
Private mcolComboboxes As Collection
'Events the Form object will see.
'In this sample we pass the instance of the ComboBox class but
'they can be easily modified to pass the actual control or the
'Index of the collection or whatever your needs.
Event ComboboxChange(ComboBoxClass As clsComboBox)
Event ComboBoxClick(ComboBoxClass As clsComboBox)
Public Function Add(ComboBoxControl As VB.ComboBox)
Dim objNew As clsComboBox
Set objNew = New clsComboBox
Set objNew.ComboBox = ComboBoxControl
Set objNew.Parent = Me
mcolComboboxes.Add objNew
Set objNew = Nothing
End Function
'should only be called by the clsComboBox module and if this were a Dll
'I would declare them as Friend Sub instead
Public Sub RaiseEvents(ComboBoxClass As clsComboBox, EventName As String)
Select Case EventName
Case "Change"
RaiseEvent ComboboxChange(ComboBoxClass)
Case "Click"
RaiseEvent ComboBoxClick(ComboBoxClass)
End Select
End Sub
Public Function Item(Index As Long) As clsComboBox
Set Item = mcolComboboxes(Index)
End Function
Private Sub Class_Initialize()
Set mcolComboboxes = New Collection
End Sub
'**** Code for the Form
Private WithEvents ComboBoxes As colComboboxes
Private Sub Form_Load()
Set ComboBoxes = New colComboboxes
'add two comboboxes to the form using the classes
ComboBoxes.Add Me.Controls.Add("VB.ComboBox", "Combo1")
With ComboBoxes.Item(1).ComboBox
.Move 0, 0, 2000
.Visible = True
.AddItem "1"
.AddItem "2"
.AddItem "3"
.AddItem "4"
End With
ComboBoxes.Add Me.Controls.Add("VB.ComboBox", "Combo2")
With ComboBoxes.Item(2).ComboBox
.Move 0, 500, 2000
.Visible = True
.AddItem "Red"
.AddItem "Green"
.AddItem "Yellow"
.AddItem "Blue"
End With
End Sub
'Events raised by the collection class
Private Sub ComboBoxes_ComboBoxClick(ComboBoxClass As clsComboBox)
Debug.Print ComboBoxClass.ComboBox.Name, ComboBoxClass.ComboBox.Text
End Sub
Private Sub ComboBoxes_ComboboxChange(ComboBoxClass As clsComboBox)
Debug.Print ComboBoxClass.ComboBox.Name, ComboBoxClass.ComboBox.Text
End Sub
Private Sub Form_Unload(Cancel As Integer)
'make sure everything is destroyed.
'probably should include code to call Controls.Remove
'for each control added at runtime.
Set ComboBoxes = Nothing
End Sub