动态添加userform文本框控件_click事件没有触发? (但_change工作)(VBA)

时间:2016-08-07 16:49:44

标签: excel vba excel-vba

所以我试图让_click事件处理用户窗体上动态添加的文本框控件。

这是我的代码,它使_change事件正常但_click事件不会触发。

' 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问题(我在哪里可以找到每个MSForms控件的所有可能事件的列表?)

1 个答案:

答案 0 :(得分:4)

MSForms.TextBox没有Click事件。但是MouseUP事件可用。

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

在对象导航框中设置所需对象后,您可以使用VBA编辑器中声明/过程导航框中的列表获得可能事件列表:

enter image description here