Multiple With Events

AlexanderBB

Well-known Member
Joined
Jul 1, 2009
Messages
1,835
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
I'm creating a number of List Boxes at run time, depending on Side
VBA Code:
Set cControl = MyForm.Frame1.Controls.Add("Forms.ListBox.1", "lst" & Side, True)
This is working ok, but I've struck unknown territory adding their Click Event code.
After some googling I've managed to get one listbox event working. In Module Declarations I have Public ObjEvents() As New clsEvents.

Then after the line above where cControl is Set
VBA Code:
Set ObjEvents.lbEvents = cControl
Then in class module
VBA Code:
Option Explicit
Public WithEvents lbEvents As MSForms.ListBox
Private Sub lbEvents_Click()
    MsgBox "Clicked"
End Sub

How can I modify this so it works with all Listboxes, "lst1", "lst2" etc. as per value of Side ?
Thanks for any help. First time using WithEvents.
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Try this option:

In Module Declarations Change Public by Dim

I don't know how your Side variable works, but I guess it is increasing from 1 to 1.

VBA Code:
Dim ObjEvents() As New clsEvents

Private Sub CommandButton1_Click()
  Dim cControl As MSForms.ListBox

  Set cControl = MyForm.Frame1.Controls.Add("Forms.ListBox.1", "lst" & Side, True)
  ReDim Preserve ObjEvents(1 To Side)
  Set ObjEvents(Side).lbEvents = MyForm.Controls("lst" & Side)
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,436
Messages
6,124,869
Members
449,192
Latest member
MoonDancer

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top