Ok, maybe it's kinda pricky, but I'm answering my question to document my turnaround on this issue.
The other fellows' answers dismissed my expectations of functional Enter
and Exit
events in event handler class modules, so here is what I did, and it seems to work out.
The basic thinking is as follows: I'm trying to trigger functions which make pleasant transition animations between controls, so I can assume I will only need to use them: (a) When the user manually goes from a control to another (if the code fills anything automatically, I won't use the animation, just put the end markup via specific code); and (b) When these input controls are selectable (viz., have TabStop = True
).
The user manually goes from a control to another by clicking on them and pressing Tab
or Shift + Tab
. I'm able to track these events in event handler class modules, via KeyUp
(worked better than KeyDown
for me), MouseDown
(didn't need to try MouseUp
), and Click
(in the command button controls).
My form have 12 static controls with TabStop (5 textboxes, 5 comboboxes and 2 command buttons), and might be created more 5 dynamically (3 textboxes and 2 comboboxes).
Thus I've created 3 event handler class modules (for command buttons, comboboxes and textboxes) to track pseudo-Enter and pseudo-Exit events. In fact, these event handlers listen to when the user clicks a control or presses tab or shift + tab (after all, these are the ways to go to another control manually). In order to know if the user went to another control or if he just clicked the same he was already in, I'd defined 2 global object variables called oActiveControl
and oPreviousControl
, which serves as tokens. If the control clicked/tabbed in is different from the oActiveControl
, this means the user have changed controls, so I need to update my tokens.
This worked really well (for these measly 4 hours; it was not hard tested). Here is the code:
''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''' ANY MODULE ''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''
Public oActiveControl As MSForms.Control, oPreviousControl As MSForms.Control
''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''' USER FORM MODULE '''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub UserForm_Initialize()
' On loading the UserForm, we are going to create the event handlers for the static controls.
Dim oControl As MSForms.Control
Dim oEvHandlerExitText As EvHndlExitTxt
Dim oEvHandlerExitCombo As EvHndlExitCbx
Dim oEvHandlerExitButton As EvHndlExitBtn
Dim colEventHandlers as Collection
Set colEventHandlers = New Collection
' Loop to create the event handlers and store them in a collection
For Each oControl In Me.Controls
Select Case TypeName(oControl)
Case "TextBox"
If oControl.TabStop = True Then
Set oEvHandlerExitText = New EvHndlExitTxt
Set oEvHandlerExitText.TextBox = oControl
colEventHandlers.Add oEvHandlerExitText
End If
Case "ComboBox"
If oControl.TabStop = True Then
Set oEvHandlerExitCombo = New EvHndlExitCbx
Set oEvHandlerExitCombo.ComboBox = oControl
colEventHandlers.Add oEvHandlerExitCombo
End If
Case "CommandButton"
If oControl.TabStop = True Then
Set oEvHandlerExitButton = New EvHndlExitBtn
Set oEvHandlerExitButton.Button = oControl
colEventHandlers.Add oEvHandlerExitButton
End If
End Select
Next oControl
End Sub
Private Sub UserForm_AddControl(ByVal Control As MSForms.Control)
' Whenever we add a control to the UserForm, if it is a Combobox, Textbox
' or Button, we are also creating their event handlers.
Select Case TypeName(Control)
Case "TextBox"
If Control.TabStop = True Then
Dim oEvHandlerExitText As EvHndlExitTxt
Set oEvHandlerExitText = New EvHndlExitTxt
Set oEvHandlerExitText.TextBox = Control
colEventHandlers.Add oEvHandlerExitText
End If
Case "ComboBox"
If Control.TabStop = True Then
Dim oEvHandlerExitCombo As EvHndlExitCbx
Set oEvHandlerExitCombo = New EvHndlExitCbx
Set oEvHandlerExitCombo.ComboBox = Control
colEventHandlers.Add oEvHandlerExitCombo
End If
Case "CommandButton"
If Control.TabStop = True Then
Dim oEvHandlerExitButton As EvHndlExitBtn
Set oEvHandlerExitButton = New EvHndlExitBtn
Set oEvHandlerExitButton.Button = Control
colEventHandlers.Add oEvHandlerExitButton
End If
End Select
End Sub
Private Sub UserForm_Terminate()
' On unloading the form, we need to dump the global token variables
Set oActiveControl = Nothing
Set oPreviousControl = Nothing
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''' CLASS MODULE EvHndlExitBtn ''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''
Option Explicit
Private WithEvents moBtn As MSForms.CommandButton
Public Property Set Button(ByVal oBtn As MSForms.CommandButton)
Set moBtn = oBtn
End Property
' The command buttons have no transitions, so there are no functions in pseudo-Enter nor
' pseudo-Exit events. This class module is here only to detect clicks or tabs which leaves
' other controls.
Private Sub moBtn_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = 9 Or KeyCode = 16 Then ' 9 = Tab, 16 = Shift + Tab
If oActiveControl Is Nothing Then Set oPreviousControl = oActiveControl
Set oActiveControl = moBtn
End If
End Sub
Private Sub moBtn_Click()
If oActiveControl Is Nothing Then Set oPreviousControl = oActiveControl
Set oActiveControl = moBtn
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''' CLASS MODULE EvHndlExitTxt ''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''
Option Explicit
Private WithEvents moTxtBox As MSForms.TextBox
Public Property Set TextBox(ByVal oTxtBox As MSForms.TextBox)
Set moTxtBox = oTxtBox
End Property
Private Sub moTxtBox_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = 9 Or KeyCode = 16 Then ' 9 = Tab, 16 = Shift + Tab
If Not oActiveControl Is Nothing Then Set oPreviousControl = oActiveControl
Set oActiveControl = moTxtBox
If Not oActiveControl Is oPreviousControl Then ' If user changed controls...
' Here is the pseudo-Enter event from the TextBox reached via Tab/Shift+Tab:
' Instructions... (these instructions will have to be repeated below, so
' Instructions... consider creating a function if there are many of them.)
If Not oPreviousControl Is Nothing Then
' Here is the pseudo-Exit event from the previous control:
' Instructions... (Use oPreviousControl to refer the control which lost focus.
' Instructions... Will have to be repeated below also)
End If
End If
End If
End Sub
Private Sub moTxtBox_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
If Not oActiveControl Is Nothing Then Set oPreviousControl = oActiveControl
Set oActiveControl = moTxtBox
If Not oActiveControl Is oPreviousControl Then ' If user changed controls...
' Here is the pseudo-Enter event from the TextBox reached via mouse clicking:
' Instructions... (these instructions where repeated above, so consider
' Instructions... creating a function if there are many of them.)
If Not oPreviousControl Is Nothing Then
' Here is the pseudo-Exit event from the previous control:
' Instructions... (Use oPreviousControl to refer the control which lost focus.
' Instructions... Was repeated above also)
End If
End If
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''' CLASS MODULE EvHndlExitCbx ''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''
Option Explicit
Private WithEvents moCmbBox As MSForms.ComboBox
Public Property Set ComboBox(ByVal oCmbBox As MSForms.ComboBox)
Set moCmbBox = oCmbBox
End Property
Private Sub moCmbBox_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = 9 Or KeyCode = 16 Then
If Not oActiveControl Is Nothing Then Set oPreviousControl = oActiveControl
Set oActiveControl = moCmbBox
If Not oActiveControl Is oPreviousControl Then ' If user changed controls...
' Here is the pseudo-Enter event from the ComboBox reached via Tab/Shift+Tab:
' Instructions... (these instructions will have to be repeated 4 times, so
' Instructions... consider creating a function if there are many of them.)
If Not oPreviousControl Is Nothing Then
' Here is the pseudo-Exit event from the previous control:
' Instructions... (Use oPreviousControl to refer the control which lost focus.
' Instructions... Will have to be repeated below also)
End If
End If
End If
End Sub
Private Sub moCmbBox_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
If Not oActiveControl Is Nothing Then oPreviousControl = oActiveControl
Set oActiveControl = moCmbBox
If Not oActiveControl Is oPreviousControl Then ' If user changed controls...
' Here is the pseudo-Enter event from the ComboBox reached via mouse clicking:
' Instructions... (these instructions where repeated above, so consider
' Instructions... creating a function if there are many of them.)
If Not oPreviousControl Is Nothing Then
' Here is the pseudo-Exit event from the previous control:
' Instructions... (Use oPreviousControl to refer the control which lost focus.
' Instructions... Was repeated above also)
End If
End If
End Sub
To save lines of code, I've adapted Gary McGill's majestic response on this question: VBA: Using WithEvents on UserForms
Thanks, Chris Nielsen and Variatus! Your explanations helped me very much! If someone have some neater answer to this issue, please post it here.
EDIT: Thanks again, Chris, for noticing the problem I couldn't see on comparing objects!