6

Hail, everybody!

I have a form in which I need to add and remove some TextBox and Label controls dynamically.

Each of these controls to be added will have 3 events also: some validation in case of a KeyPress and some other stuff on Enter and Exit. The problem is the events Enter and Exit seems not to work inside the event handler class module.

The same project have other event handler modules, and they work nice (but they have no Enter nor Exit events).

The code in the event handler class module is as follows. I won't post the code in the form module because it's working fine, it's just something about Enter and Exit events inside event handlers.

Do you know any way to trigger actions on Enter and Exit events of a dynamically added TextBox?

EDIT: My objective is to produce some cool transition animation when the TextBox gets focused and when it lost focus. This animation already occurs with the pre-existing controls; it's a function triggered on the Enter and Exit events (GotFocus and LostFocus would be nice, but are also not available).

Option Explicit

Private WithEvents moTextDate As MSForms.TextBox

Public Property Set DateTextBox(ByVal oTxtBox As MSForms.TextBox)
    Set moTextDate = oTxtBox
End Property

Private Sub moTextDate_Enter()
    ' Things to do on receiving focus
End Sub

Private Sub moTextDate_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    If ValidateNumbers(KeyAscii, "/", ":", " ") = False Then KeyAscii = 0
End Sub

Private Sub moTextDate_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    ' Things to do when lost focus
End Sub
5
  • 1
    Indeed. You are right and event procedures created before their textbox won't fire. It used to work. Now I took down my answer. I shall look for a better answer for you.
    – Variatus
    Commented Aug 24, 2020 at 7:20
  • 1
    I didn't find a way to make the Enter event available in the method you chose (which appears to be the correct one). So, what I recommend is to create the controls you want, along with their event procedures, and hide them in the initialize event, making them visible when you need them. Whenever I used this method I disabled and sometimes moved them out of the way and/or resized them. But more recently I didn't bother, to no ill effect. It seems once invisible you can click right through them.
    – Variatus
    Commented Aug 24, 2020 at 7:40
  • Ok, this probably will work. Thank you! I'm trying some workaround whose edges I'm still trimming, and if everything works out it will be a more economic code, which I will describe soon. If it doesn't work, I'll try this way. Commented Aug 24, 2020 at 10:18
  • 2
    Have a look here: for Exit Enter and other events: stackoverflow.com/questions/61855925/…
    – EvR
    Commented Aug 25, 2020 at 15:50
  • 1
    Wow! It would be what I wanted from the beginning! I won't change it in this project, because my workaround fits my application perfectly, but when I need Enter and Exit events dynamically again, it will be this way. Thanks! Commented Aug 26, 2020 at 0:28

2 Answers 2

2

Unfortunately the list of Events exposed by the Event Handler Class method does not include Enter or Exit events. You can see what events are available in the IDE

enter image description here

Without knowing more about your project, it's hard to recommend any alternatives.

1
  • I've edited the question to explain what I want to do. My goal is to call a function which makes a pleasant transition when the textboxes gains and losts focus. Commented Aug 24, 2020 at 7:20
2

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!

2
  • 1
    Good to see you've got a solution that works for you. One minor point: you say For some reason, the comparison doesn't work between the control objects, but worked with their names Did you try If Not oActiveControl Is moCxTexto Then ? Commented Aug 25, 2020 at 0:22
  • Right, I'm comparing objects! Duh! REALLY thanks, buddy! Commented Aug 25, 2020 at 4:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.