简体   繁体   English

用户窗体中 Combobox 的更改事件未被触发

[英]Change event of Combobox in Userform not getting triggered

I have a userform and a Click button on it.我有一个用户表单和一个点击按钮。 On click of this button a combobox is dynamically created.单击此按钮会动态创建一个 combobox。 I want to do something when a particular value is selected from this combobox but the change event is not getting triggered.当从这个 combobox 中选择一个特定值时,我想做一些事情,但没有触发更改事件。 What could be the reason.可能是什么原因。 Here is my code which is put in the UserForm1 module.这是我放在 UserForm1 模块中的代码。

Private WithEvents ComboBox1 As MSForms.ComboBox

Private Sub ClickButton_Click()
      
    'Create  combo box
    Dim ComboBox1 As MSForms.ComboBox
    Set ComboBox1 = Me.Controls.Add("Forms.ComboBox.1")
    With ComboBox1
        .Left = 160
        .Top = 50
        .Width = 70
        .Height = 20
        .AddItem ("> than")
        .AddItem ("< than")
        .AddItem ("Max")
        .AddItem ("Min")
        .Enabled = True
        .BackColor = RGB(255, 255, 255)
        .ForeColor = RGB(0, 0, 0)
        .SpecialEffect = fmSpecialEffectFlat
        .Font.Size = 12
        .Font.Bold = False
        .Font.Name = "Arial"
        .TabIndex = 2
    End With
        
    DoEvents
    ComboBox1.SetFocus
      
End Sub

Private Sub ComboBox1_Change()
    Dim inputNumber As Variant
    If ComboBox1.Value = "> than" Then
        inputNumber = InputBox("Enter a number:")
        'Check if the input is valid number
        If IsNumeric(inputNumber) Then
            ComboBox1.Value = ComboBox2.Value & " " & inputNumber
        Else
            MsgBox "Invalid input"
        End If
    End If
End Sub

The method you need to use is described here: https://stackoverflow.com/a/8986622/9852011 , but for your particular case, here is what you need to do:此处描述了您需要使用的方法: https://stackoverflow.com/a/8986622/9852011 ,但对于您的特定情况,您需要执行以下操作:

This is the code that should be in the module of your UserForm:这是应该在用户窗体模块中的代码:

Private m_oCollectionOfEventHandlers As Collection

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

Private Sub CommandButton1_Click()
    Dim ComboBox1 As MSForms.ComboBox
    Set ComboBox1 = Me.Controls.Add("Forms.ComboBox.1")
    With ComboBox1
        .Left = 160
        .Top = 50
        .Width = 70
        .Height = 20
        .AddItem ("> than")
        .AddItem ("< than")
        .AddItem ("Max")
        .AddItem ("Min")
        .Enabled = True
        .BackColor = RGB(255, 255, 255)
        .ForeColor = RGB(0, 0, 0)
        .SpecialEffect = fmSpecialEffectFlat
        .Font.Size = 12
        .Font.Bold = False
        .Font.Name = "Arial"
        .TabIndex = 2
    End With
        
    DoEvents
    ComboBox1.SetFocus
    
    Dim cb1EventHandler As comboboxeventhandler
    Set cb1EventHandler = New comboboxeventhandler
    Set cb1EventHandler.ComboBox = ComboBox1
    m_oCollectionOfEventHandlers.Add cb1EventHandler
      
End Sub

Then, insert a new class module into your project, name it "ComboBoxEventHandler" and put this code into it:然后,将一个新的 class 模块插入到您的项目中,将其命名为“ComboBoxEventHandler”并将以下代码放入其中:

Private WithEvents m_oComboBox As MSForms.ComboBox

Public Property Set ComboBox(ByVal oComboBox As MSForms.ComboBox)
    Set m_oComboBox = oComboBox
End Property

Private Sub m_oComboBox_Change()
    Dim inputNumber As Variant
    With m_oComboBox
        If .Value = "> than" Then
            inputNumber = InputBox("Enter a number:")
            'Check if the input is valid number
            If IsNumeric(inputNumber) Then
                .Value = .Parent.ComboBox2.Value & " " & inputNumber
            Else
                MsgBox "Invalid input"
            End If
        End If
    End With
End Sub

I don't know what "ComboBox2" is but for the sake of this example, I just assumed it is a ComboBox which already exists in the UserForm somewhere.我不知道“ComboBox2”是什么,但为了这个例子,我假设它是一个 ComboBox,它已经存在于 UserForm 的某个地方。

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

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