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.
I guess my comment was not clear enough. Of course one also has to remove or replace
Set colButtons = New Collectionotherwise 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
Another approach would be to check if there is already an active instance like that
I'd also recommend to do some reading on scope of variables and objects