如何使用带有控件集合的类 [英] How to use a Class with a Collection of Controls
本文介绍了如何使用带有控件集合的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试调整下面链接中的解决方案,以使仅允许数字的文本框集合成为可能.我没有收到任何错误,但该类仅适用于文本框.
I tried adapting the solution in the link below to make a collection of text boxes allow numbers only. I get no error but the class just doesn't apply to the textboxes.
Class Module
Class Module
Public WithEvents TextGroup As MSForms.TextBox
Public Property Set Control(tb As MSForms.TextBox)
Set TextGroup = tb
End Property
Private Sub TextGroup_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case 48 To 57
Case Else
KeyAscii = 0
End Select
End Sub
UserForm
Dim tbCollection As Collection
Private Sub UserForm_Initialize()
Dim obj As clsTextBox
Dim ctrl As Control
Set tbCollection = New Collection
tbCollection.Add Me.tbAC
tbCollection.Add Me.tbCR
tbCollection.Add Me.tbHP
For Each ctrl In tbCollection
Set obj = New clsTextBox
Set obj.Control = ctrl
Next
End Sub
推荐答案
您需要将obj对象放入集合中,而不是控件本身
You need to put the obj objects in the collection, not the controls themselves
未经测试:
Dim tbCollection As Collection
Private Sub UserForm_Initialize()
Dim obj As clsTextBox
Dim arr
Dim ctrl
Set tbCollection = New Collection
arr = Array(Me.tbAC, Me.tbCR, Me.tbHP) '<< edit: no Set
For Each ctrl in arr
Set obj = New clsTextBox
Set obj.Control = ctrl
tbCollection.Add obj
Next
End Sub
这篇关于如何使用带有控件集合的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文