Home > database >  Perform similar mouse events for all the labels in userform using vba class module
Perform similar mouse events for all the labels in userform using vba class module

Time:08-19

I have three labels in my Userform. Label Names:

  • GTO25
  • GTO35
  • GTO45

I want to perform similar actions on all three: i.e

  • Click_events
  • Mouse_move_events
  • Caption_change_events (Change event I don't know if this exist on label?).

To start with the click event, I created a Class Module and added this coded to handle Click_Event, however this did not work for me. Class Name "GTOLabels":

Option Explicit
Public WithEvents mLabelGroup As MSForms.Label
Private Sub mLabelGroup_Click()
    MsgBox mLabelGroup.Name & " has been pressed"
End Sub

For the mouse move event, it should show the listbox name "GTOList".

    Set cGTO = mLabelGroup
    
    GTOList.Top = mLabelGroup.Top   15
    GTOList.Left = mLabelGroup.Left
    GTOList.Width = 47
    GTOList.Visible = True

At userform Initialization() I added these lines, to assign labels to class "GTOLabels".

    Dim cLblEvents As GTOLabels
    Dim lbl As MSForms.Label
    Dim mColEvents As Collection
    
    Set mColEvents = New Collection 'not sure!
    
    Set lbl = Me.Controls("GTO45")
    Set cLblEvents = New GTOLabels
    Set cLblEvents.mLabelGroup = lbl 'not sure!
    mColEvents.Add cLblEvents 'not sure!
    
    Set lbl = Me.Controls("GTO25")
    Set cLblEvents = New GTOLabels 
    Set cLblEvents.mLabelGroup = lbl 'not sure!
    mColEvents.Add cLblEvents 'not sure!
    
    Set lbl = Me.Controls("GTO35")
    Set cLblEvents = New GTOLabels
    Set cLblEvents.mLabelGroup = lbl 'not sure!
    mColEvents.Add cLblEvents 'not sure!

I am not sure that it is assigning the labels to class here. I have checked the click event is not working neither mouse move. what I am doing it wrong?

I can declared "public cGTO" in userform, and set it to the current cLabel when mouse_move on as you can see in the above code. I need that label caption to be changed as GTOList_change event happened. is there another way around? i.e whatever value in the GTOList has the current label should have that value. I am adding an image to show.

Remember there are 3 labels, but only one listbox.

enter image description here

CodePudding user response:

Firstly, Caption_change_event does not exist...

Please, try the next adapted solution:

  1. The "GTOLabels" class code should look as (to trigger both events):
Option Explicit

Public WithEvents mLabelGroup As MSForms.Label

Private Sub mLabelGroup_Click()
    If left(mLabelGroup.Caption, 10) = "My Caption" Then
        mLabelGroup.Caption = "Changed Caption"
    Else
        mLabelGroup.Caption = "My Caption " & Right(mLabelGroup.name, 2)
    End If
    MsgBox mLabelGroup.name & " has been pressed"
End Sub

Private Sub mLabelGroup_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
     Dim listB As MSForms.listbox
     Set listB = mLabelGroup.Parent.GTOList
     With listB
            .top = mLabelGroup.top   15
            .left = mLabelGroup.left
            .width = 47
     End With
End Sub
  1. The Initialize event code should look like:
Option Explicit

Private cLblEvents As GTOLabels, mColEvents As Collection

Private Sub UserForm_Initialize()
   Dim ctrl As MSForms.Control
    Set mColEvents = New Collection
    
    For Each ctrl In Me.Controls
        If TypeName(ctrl) = "Label" And left(ctrl.name, 3) = "GTO" Then
            Set cLblEvents = New GTOLabels
            Set cLblEvents.mLabelGroup = ctrl
            mColEvents.Add cLblEvents
        End If
    Next ctrl
End Sub
  1. Now, as i said above, Caption_change_event does not exist and it will be useless, anyhow, since you cannot manually changing it to trigger an event...

But you can change the label even in code, whenever you want. For instance, the Click event can contain code lines to do that. In fact, I will adapt the class code for this event to show you what I mean...

Now, play with controls in discussion and see that the events have been triggered.

  • Related