Multiple listbox control - listboxes on multipages

RawlinsCross

Active Member
Joined
Sep 9, 2016
Messages
437
So I'm trying tie in 7 listboxes to a class for common behaviour. I had this working before, but this is the first time I'm using it with a multipage configuration. Maybe I'm grasping for straws here but it won't recognize the "SelectedListBox" name property? Can anyone ascertain why?

Main Userform Code
VBA Code:
Private ListBoxControls() As New clsListBoxControl
Private msSelectedListBox As String


'Userform public properties
Public Property Let SelectedListBox(sSelectedListBox As String)
    msSelectedListBox = sSelectedListBox
End Property

'Userform methods

Private Sub Userform_Initialize()

Call PopulateControlArrays

End Sub


Private Sub PopulateControlArrays()

Dim ctrl As control
Dim lListBoxCount As Long

For Each ctrl In Me.Controls
    If TypeName(ctrl) = "ListBox" Then
        lListBoxCount = lListBoxCount + 1
        ReDim Preserve ListBoxControls(1 To lListBoxCount)
        Set ListBoxControls(lListBoxCount).ListBoxControlGroup = ctrl
        Set ListBoxControls(lListBoxCount).Parent = Me
    End If
Next ctrl
Stop
End Sub

Public Sub ClearListBoxSelection()

Dim i As Long

For i = 0 To Me.Controls(msSelectedListBox).ListCount - 1
    If Me.Controls(msSelectedListBox).Selected(i) = True Then
        Me.Controls(msSelectedListBox).Selected(i) = False
    End If
Next i

End Sub

Class Module
VBA Code:
Option Explicit
 
Public WithEvents ListBoxControlGroup As MSForms.ListBox
Private mfrmParent As UserForm

'--public properties
Public Property Set Parent(frmParent As UserForm)
 Set mfrmParent = frmParent
End Property


Private Sub ListBoxControlGroup_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As Single, ByVal Y As Single)

ListBoxControlGroup.mfrmParent.SelectedListBox = ListBoxControlGroup.Name  <---- Fails here, "Object doesn't support this object or method"

If Button = 2 Then
    Call ListBoxControlGroup.mfrmParent.ClearListBoxSelection
End If

End Sub
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
First of all, you should be declaring your UserForm variables as the specific data type for your UserForm, not as a generic one as you've done in your class module. Otherwise, the Name property won't be available. So, for example, if your UserForm is called UserForm1, you would declare your variables as UserForm1, not UserForm.

Secondly, ListBoxControlGroup refers to the ListBox contained in the class object, so ListBoxControlGroup.mfrmParent will cause an error. However, you can simply refer to your UserForm as mfrmParent.

So let's assume that the name of your UserForm is UserForm1. In this case, the code for your class module would be as follows...

VBA Code:
Option Explicit
 
Public WithEvents ListBoxControlGroup As MSForms.ListBox
Private mfrmParent As UserForm1

'--public properties
Public Property Set Parent(frmParent As UserForm1)
 Set mfrmParent = frmParent
End Property


Private Sub ListBoxControlGroup_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As Single, ByVal Y As Single)

mfrmParent.SelectedListBox = ListBoxControlGroup.Name

If Button = 2 Then
    Call mfrmParent.ClearListBoxSelection
End If

End Sub

Hope this helps!
 
Last edited:
Upvote 0
Hi Domenic... worked like a charm. But what happens when another (say similar) userform with its own listboxes needs to use the same Class?
 
Upvote 0
If you're wanting something generic to reuse, you wouldn't tightly couple things as you have now, you'd use events and an intermediary - have a look here: Event handler

A simple way of soling the above though, would be to simply clear the listbox in the event handling class rather than calling the parent
 
Upvote 0
Hi Domenic... worked like a charm. But what happens when another (say similar) userform with its own listboxes needs to use the same Class?

In that case, in your class module, you can replace...

VBA Code:
Private mfrmParent As UserForm1

with

VBA Code:
Private mfrmParent As Object

and

VBA Code:
Public Property Set Parent(frmParent As UserForm1)

with

VBA Code:
Public Property Set Parent(frmParent As Object)

Also, as @Kyle123 has already mentioned, you can clear the ListBox in the event handling class. So depending on what you're doing, you may find that there's no need for a Parent property for your class.

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,215,836
Messages
6,127,173
Members
449,368
Latest member
JayHo

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