Alhn
Alhn

Reputation: 21

Class event on userform in modeless

The event assigned to dynamically created buttons works when showmodal=True.

I need to work with the application while the userform is open.

Below are the sub in module and class module.

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
My case should be much simpler.

Upvotes: 2

Views: 95

Answers (1)

Storax
Storax

Reputation: 12167

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

Upvotes: 0

Related Questions