Programmatically add ListBox Event

Salamander

Board Regular
Joined
Jul 30, 2009
Messages
64
Hi all,

I have a small userform which currently programatically adds a multipage page for each open workbook. Onto this new page it adds a new listbox control and populates it with appropriate items.

Code:
With Me.MultiPage1
   .Pages.Add
   .Pages(i - 2).Caption = Application.Workbooks(i).Name
   Set lb = Me.MultiPage1.Pages(i - 2).Controls.Add("Forms.Listbox.1")
   For Each ws In Application.Workbooks(i).Worksheets
      lb.AddItem ws.Name
   Next ws
End With

This is all simple enough and works no problem, however:

Is it possible to then create a listbox_DblClick event to this new listbox?

Any suggestions greatly appreciated!


Cheers,

S.
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Do all your listboxes do basically the same thing when double-clicked? If so, I would use a class containing a WithEvents Listbox variable, and store each instance of the class in a collection in your userform. For example:
Class module called CListboxHandler:
Code:
Option Explicit

Public WithEvents SheetList As MSForms.ListBox

Private Sub SheetList_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
    MsgBox SheetList.Value
End Sub

now at the top of your form module add:
Code:
Dim colListboxes as Collection
then in the code that creates the listboxes you need something like this:
Code:
Dim objCLB as CListboxHandler
Set colListboxes = New Collection
With Me.MultiPage1
   .Pages.Add
   .Pages(i - 2).Caption = Application.Workbooks(i).Name
   Set lb = Me.MultiPage1.Pages(i - 2).Controls.Add("Forms.Listbox.1")
   For Each ws In Application.Workbooks(i).Worksheets
      lb.AddItem ws.Name
   Next ws
   Set objCLB = New CListboxHandler
   Set objCLB.SheetList = lb
   colListboxes.Add objCLB
End With
 
Upvote 0
Thanks Rory - That's absolutely fantastic - works like a charm =]

If so, I would use a class containing a WithEvents Listbox variable, and store each instance of the class in a collection in your userform.

I think I really need to brush up on classes and collections - studied them when I was initially learning to code but have only used them *very* sparingly. This is my homework!

Thanks again =]

S.
 
Upvote 0

Forum statistics

Threads
1,216,212
Messages
6,129,531
Members
449,515
Latest member
lukaderanged

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