简体   繁体   English

动态添加的用户窗体文本框控件_click事件未触发? (但_change有效)(VBA)

[英]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. 因此,我正在尝试使_click事件在用户窗体上的动态添加的文本框控件上工作。

Here is my code, it gets the _change events just fine but the _click event do not fire. 这是我的代码,它可以很好地获取_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 question (where can I find the list of all possible events for each of the MSForms controls ? )) (奖励问题(我在哪里可以找到每个MSForms控件的所有可能事件的列表?)

A MSForms.TextBox has not a Click event. MSForms.TextBox没有Click事件。 But a MouseUP event is available. 但是有一个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

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: 在“对象”导航框中设置所需的对象后,可以使用VBA编辑器中“声明/过程”导航框中的列表来列出可能的事件:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM