Class module sharing between Labels and CommandButtons

RawlinsCross

Active Member
Joined
Sep 9, 2016
Messages
437
I have a series of labels on one userform and a series commandbuttons on another userform. They both contain properties that I want to access through a Class Module (see below). At the moment I have to apply them to two separate class modules (i.e. one for labels and one for command buttons) - is there a way to combine them?

Class module for label
VBA Code:
Option Explicit

Public WithEvents ElementButtonGroup As MSForms.Label
Private mfrmParent As Object

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

'--event procedures
Private Sub ElementButtonGroup_Click()

mfrmParent.SelectedElement = ElementButtonGroup.Caption
mfrmParent.ElementLabel = ElementButtonGroup.Name
mfrmParent.ElementSet

End Sub

Class module for CommandButton
VBA Code:
Option Explicit

Public WithEvents ElementChoiceGroup As MSForms.CommandButton
Private mfrmParent As Object

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

'--event procedures
Private Sub ElementChoiceGroup_Click()

mfrmParent.SelectedElement = ElementChoiceGroup.Caption
mfrmParent.ElementLabel = ElementChoiceGroup.Name
mfrmParent.ElementSet

End Sub
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Yes, you could add a property to set the control object, and assign it to the relevant control type. For example:

Code:
Option Explicit

Private WithEvents ElementButtonGroup As MSForms.Label
Private WithEvents ElementChoiceGroup As MSForms.CommandButton
Private mfrmParent As Object

'--public properties
Public Property Set Parent(frmParent As Object)
Set mfrmParent = frmParent
End Property
Public Property Set Control(ctl As Object)
Select case lcase$(typename(ctl))
case "label"
set ElementButtonGroup = ctl
case "commandbutton"
set ElementChoiceGroup = ctl
End Select
End Property
'--event procedures
Private Sub ElementButtonGroup_Click()

mfrmParent.SelectedElement = ElementButtonGroup.Caption
mfrmParent.ElementLabel = ElementButtonGroup.Name
mfrmParent.ElementSet

End Sub
Private Sub ElementChoiceGroup_Click()

mfrmParent.SelectedElement = ElementChoiceGroup.Caption
mfrmParent.ElementLabel = ElementChoiceGroup.Name
mfrmParent.ElementSet

End Sub
 
Upvote 0
How then would I augment the assigning of controls from one of the userforms to this new class... this is my existing code from the userform that has labels:

VBA Code:
Private Sub PopulateControlArrays()

Dim ctrl As Control
Dim lElementButtonCount As Long

For Each ctrl In Me.Controls
   If TypeName(ctrl) = "Label" Then
      If ctrl.Tag = "ElementButton" Then
          lElementButtonCount = lElementButtonCount + 1
          ReDim Preserve ElementButtons(1 To lElementButtonCount)
          Set ElementButtons(lElementButtonCount).ElementButtonGroup = ctrl
          Set ElementButtons(lElementButtonCount).Parent = Me
      End If
   End If
Next ctrl

End Sub
 
Upvote 0
You'd use the new class type and change this:

Code:
Set ElementButtons(lElementButtonCount).ElementButtonGroup = ctrl

to this:

Code:
Set ElementButtons(lElementButtonCount).Control = ctrl
 
Upvote 0
Because I can never remember how they’re all formatted! :)
 
Upvote 0

Forum statistics

Threads
1,215,540
Messages
6,125,409
Members
449,223
Latest member
Narrian

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