On a worksheet I have 3 ActiveX objects being TextBox1, TextBox2, ListBox1
Leaving other code out I have a class clsEvents that contains
Private WithEvents txbControl As MSForms.TextBox
Private WithEvents lisControl As MSForms.ListBox
Private txbEvents As TextBoxEvents
Private lisEvents As ListBoxEvents
Private Sub txbControl_Change()
txbEvents.ChangeEvent txbControl
End Sub
Private Sub lisControl_Change()
lisEvents.ChangeEvent lisControl
End Sub
and the classes TextBoxEvents and ListBoxEvents contain
Public Event Changed(txtBox As MSForms.TextBox)
Public Sub ChangeEvent(txtBox As MSForms.TextBox)
RaiseEvent Changed(txtBox)
End Sub
Public Event Changed(ByRef myListBox As MSForms.ListBox)
Public Sub ChangeEvent(lisBox As MSForms.ListBox)
RaiseEvent Changed(lisBox)
End Sub
The worksheet module contains
Public WithEvents tbxEvents As TextBoxEvents
Public WithEvents lisEvents As ListBoxEvents
Private Sub tbxEvents_Changed(tbxBox As MSForms.TextBox)
Debug.Print "tbxEvents_Changed " & tbxBox.Name
End Sub
Private Sub lisEvents_Changed(lisBox As MSForms.ListBox)
Debug.Print "lisEvents_Changed " & lisBox.Name
End Sub
Private Sub TextBox2_Change()
Debug.Print "TextBox2_Change"
End Sub
Private Sub TextBox1_Change()
Debug.Print "TextBox1_Change"
End Sub
Private Sub ListBox1_Change()
Debug.Print "ListBox1_Changed "
End Sub
If I change something in TextBox1 or TextBox2 or ListBox1 the debugwindow shows that the events first will be send to the worksheet (TextBox1_Change etc.) followed by tbxEvents_Changed or LisEvents_Changed, so it it working.
What I would like to achieve is replacing the code in clsEvents by something like
Private WithEvents objControl As OLEobject
Private txbEvents As TextBoxEvents
Private lisEvents As ListBoxEvents
Private Sub objControl_Change()
if (TypeOf objControl.Object Is MSForms.TextBox) Then
txbEvents.ChangeEvent objControl
elseif (TypeOf objControl.Object Is MSForms.ListBox) Then
lisEvents.ChangeEvent objControl
endif
End Sub
So basically I would like to know how I can achieve a valid definition for WithEvents that will remove the necessity for 'many' eventfunctions in clsEvents.
Public WithEvents objControl As ?????
See Question&Answers more detail:os