You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何引用类集合中的元素?ToggleButton的sName获取报错求解

问题

我有一组包含ToggleButton及其对象名称的集合,尝试使用代码itbcollection.Item(2).sName引用集合中第二个元素的sName时,出现「Object does not support this property or method」错误,请问获取sName的正确代码是什么?

相关代码

捕获MouseDown事件的类模块(cObjectArray)代码

Option Explicit
Private WithEvents cTB  As MSForms.ToggleButton
Private sName As String

Public Property Let aTB(iTB As ToggleButton)
        Set cTB = iTB
        sName = cTB.Name
End Property

Private Sub cTB_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As Single, ByVal Y As Single)
        If Button = 1 Then Exit Sub
        Module1.RightClickDay cTB.Caption
End Sub

窗体加载时加载ToggleButton到集合的代码

Dim tbEvent As cObjectArray
Dim TB(42) As Object
    x = 0
    For Each o In UFShows.Controls
        If o.Tag = "T" Then
            Set TB(x) = o
            x = x + 1
            o.Visible = False
            Set tbEvent = New cObjectArray
            tbEvent.aTB = o
            iTBcollection.Add tbEvent
        End If
    Next o

解决方案

错误原因是sName是类模块中的私有变量,外部代码无法直接访问。需要在类模块cObjectArray中添加一个公开的Property Get方法来暴露这个变量:

在类模块中追加以下代码:

Public Property Get sName() As String
    sName = Me.sName
End Property

添加完成后,就能通过itbcollection.Item(2).sName正常获取目标元素的sName值了。

内容的提问来源于stack exchange,提问作者igittr

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 01:20:31