【发布时间】:2020-05-17 07:00:56
【问题描述】:
我有一个带有多个TextBox 的用户窗体,我需要检查它们的值,因此我使用带有WithEvent 私有对象的类模块创建了一个事件处理程序。
Change 事件处理程序可以正常工作,但 AfterUpdate 处理程序不能正常工作(BeforeEvent、Enter、Exit 也是如此)。
以下是问题的一个简短示例:
' class name is NumberBox
Private WithEvents nbTextBox As MSForms.TextBox
Public Property Set TextBox(ByVal t As MSForms.TextBox)
Set nbTextBox = t
End Property
Private Sub nbTextBox_Change()
Debug.Print "Change " & nbTextBox.Value ' Working
End Sub
Private Sub nbTextBox_AfterUpdate()
Debug.Print "AfterUpdate " & nbTextBox.Value 'not working
End Sub
用户窗体代码如下所示:
Private col As Collection
Private Sub UserForm_Initialize()
Set col = New Collection
Dim c1 As MSForms.TextBox, c2 As MSForms.TextBox
Dim tb1 As NumberBox, tb2 As NumberBox
Set c1 = Controls("TextBox1")
Set c2 = Controls("TextBox2")
Set tb1 = New NumberBox
Set tb2 = New NumberBox
Set tb1.TextBox = c1
Set tb2.TextBox = c2
col.Add tb1
col.Add tb2
End Sub
我尝试将UserForm.TextBox 更改为UserForm.Control,但我得到了object or class does not support the set of events,尽管Userform.Control 是根据文档定义AfterUpdate 事件的类。
【问题讨论】: