Shodan
Shodan

Reputation: 1158

Dynamically added userform textbox control _click event not firing ? (but _change works) (VBA)

So I'm trying to get the _click event to work on a dynamically added textbox control on a userform.

Here is my code, it gets the _change events just fine but the _click event do not fire.

' Userform code
Dim myControlsEventH As Collection

Private Sub UserForm_Initialize()
    Set myControlsEventH = New Collection
End Sub

Public Sub AddTextbox(myName As String)

    'Dim MyTextBox As Object
    Dim myTextbox As MSForms.TextBox

    Set myTextbox = frmStamps.Controls.Add("Forms.TextBox.1", myName, True)

    myTextbox.TextAlign = 2
    myTextbox.Font.Size = 18
    myTextbox.WordWrap = False
    'MyTextBox.AutoSize = True
    AdjustSize

    Dim txtbxEvent As ctxtbxEventH
    Set txtbxEvent = New ctxtbxEventH
    Set txtbxEvent.frm = frmStamps
    Set txtbxEvent.txtbox = myTextbox

    myControlsEventH.Add txtbxEvent

End Sub

' ctxtbxEventH Class code
Public WithEvents txtbox As MSForms.TextBox
Public frm As UserForm

Private Sub txtbox_Click()
    Debug.Print "clicked"
End Sub

Private Sub txtbox_Change()
    Debug.Print "changed"
End Sub

(BONUS question (where can I find the list of all possible events for each of the MSForms controls ? ))

Upvotes: 0

Views: 759

Answers (1)

Axel Richter
Axel Richter

Reputation: 61860

A MSForms.TextBox has not a Click event. But a MouseUP event is available.

Private Sub txtbox_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    Debug.Print "mouse up"
End Sub

You can have a list of possible events using the list in the Declarations/Procedure navigation box in the VBA Editor after set the needed object in the Object navigation box:

enter image description here

Upvotes: 4

Related Questions