How to get class event working on userform in modeless?

68 views Asked by At

The event assigned to dynamically created buttons working fine when showmodal=True. But what I need to do is working with the application while the userform is open. Any help will be much appreciated. Below are the sub in module and class module THank you.

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

I tried to understand and implement the solution suggested here: Functionalize Event Driven Modeless UserForm Class But it did not work for me. My case should be much simpler and Any help will bemuch appreciated.

1

There are 1 answers

0
Storax On

I guess my comment was not clear enough. Of course one also has to remove or replace Set colButtons = New Collection otherwise one will always create a new instance (is that the correct english word for this?) of the collection.

My complete suggestion would be to change the posted code as follows

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

Another approach would be to check if there is already an active instance like that

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

I'd also recommend to do some reading on scope of variables and objects