如何让类事件在无模式的用户窗体上工作?

问题描述 投票:0回答:1

当 showmodal=True 时,分配给动态创建的按钮的事件工作正常。 但我需要做的是在用户窗体打开时使用应用程序。 任何帮助都感激不尽。以下是子模块和类模块 谢谢你。

Sub tests()

    Dim i As Integer
    Dim MyB As MSForms.Control
    Dim btnEvent As MyCustomButton
    Dim colButtons As Collection
    Set colButtons = New Collection
    
    For i = 1 To 7
      
            Set MyB = UserForm1.Controls.Add("Forms.CommandButton.1")
            
            With MyB
                .Name = "dugme" & i
                .Caption = "Captiones" & i
                .Left = 10
                .Top = 25 * i
                .Width = 75
                .Height = 20
                .Tag = "tagi" & i
            End With
            
            Set btnEvent = New MyCustomButton
            Set btnEvent.btn = MyB
            Set btnEvent.frm = UserForm1
           colButtons.Add btnEvent
            
    
    Next i

UserForm1.Show

End Sub

--- class module MyCustomButton-------

Public WithEvents btn As MSForms.CommandButton
Public frm As UserForm


Private Sub btn_Click()

    MsgBox btn.Name

End Sub

我尝试理解并实现此处建议的解决方案:功能化事件驱动的无模式用户表单类 但这对我不起作用。我的情况应该更简单,任何帮助将不胜感激。

vba forms class events modeless
1个回答
0
投票

我想我的评论不够清楚。当然,还必须删除或替换

Set colButtons = New Collection
,否则总是会创建集合的一个新实例(这是正确的英文单词吗?)。

我的完整建议是更改发布的代码如下

Option Explicit
' the New keyword will take care that a new instance is created if needed
Dim colButtons As New Collection

Sub tests()

    Dim i As Integer
    Dim MyB As MSForms.Control
    Dim btnEvent As MyCustomButton
'    Dim colButtons As Collection
'    Set colButtons = New Collection
    
    For i = 1 To 7
      
            Set MyB = UserForm1.Controls.Add("Forms.CommandButton.1")
            
            With MyB
                .Name = "dugme" & i
                .Caption = "Captiones" & i
                .Left = 10
                .Top = 25 * i
                .Width = 75
                .Height = 20
                .Tag = "tagi" & i
            End With
            
            Set btnEvent = New MyCustomButton
            Set btnEvent.btn = MyB
            Set btnEvent.frm = UserForm1
           colButtons.Add btnEvent
            
    
    Next i

UserForm1.Show vbModeless

End Sub

另一种方法是检查是否已经存在这样的活动实例

Option Explicit
Dim colButtons As Collection

Sub tests()

    Dim i As Integer
    Dim MyB As MSForms.Control
    Dim btnEvent As MyCustomButton
'    Dim colButtons As Collection
'    Set colButtons = New Collection
   If colButtons Is Nothing Then
    Set colButtons = New Collection
   End If
    
    For i = 1 To 7
      
            Set MyB = UserForm1.Controls.Add("Forms.CommandButton.1")
            
            With MyB
                .Name = "dugme" & i
                .Caption = "Captiones" & i
                .Left = 10
                .Top = 25 * i
                .Width = 75
                .Height = 20
                .Tag = "tagi" & i
            End With
            
            Set btnEvent = New MyCustomButton
            Set btnEvent.btn = MyB
            Set btnEvent.frm = UserForm1
           colButtons.Add btnEvent
            
    
    Next i

UserForm1.Show vbModeless

End Sub

我还建议阅读一些关于变量范围对象

的内容
© www.soinside.com 2019 - 2024. All rights reserved.