RaiseEvent is fired only for the last control

PetLahev

New Member
Joined
Jul 30, 2009
Messages
28
Hello

I've been trying to solve this problem for hours with no success.
Please, kick me
It should be simple
1, form with labels that are added dynamicaly
2, a common class that catch labels event

I prepared a sample with one form and one class

code in the form - UserForm1
Code:
Dim storeInstances          As Collection
Dim WithEvents lblEvents    As LablesClass

Private Sub lblEvents_Clicked(labelName As String)
        MsgBox "Whooo the raise event works for " & labelName
End Sub

Private Sub UserForm_Initialize()
    
    Set storeInstances = New Collection
    
    Set lblEvents = New LablesClass
    Set lblEvents.lablesEvent = Label1
    storeInstances.Add lblEvents
    
    Set lblEvents = New LablesClass
    Set lblEvents.lablesEvent = Label2
    storeInstances.Add lblEvents
    
    Set lblEvents = New LablesClass
    Set lblEvents.lablesEvent = Label3
    storeInstances.Add lblEvents
    
End Sub
code in the class - LablesClass
Code:
Public WithEvents lablesEvent As MSForms.Label

Public Event Clicked(labelName As String)

Private Sub lablesEvent_Click()
    MsgBox lablesEvent.Name
    RaiseEvent Clicked(lablesEvent.Name)
End Sub
I'd like to achieve a result when every click on any label will raise the RaiseEvent code.

Thanks
Pat
 
But why do you need to raise an event back to the form when you have already triggered a click event for that label? It still makes no sense to me.
 
Upvote 0

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
But why do you need to raise an event back to the form when you have already triggered a click event for that label? It still makes no sense to me.

because I used a common class (now I'm going to use code that Jaafar and Mike provided) and I could not set value of an option button from that class.
I think it's not possible, is it? (look at my first post)
 
Upvote 0
Your first post only really tells me that you are trying to use RaiseEvent, not why. Given that your controls are in groups and should be treated that way, I would suggest you use a class with three labels and a checkbox. Then the labels can control their respective checkbox as required.
 
Upvote 0
On a simple level the class becomes:
Code:
Option Explicit

Public WithEvents lableName As MSForms.Label
Public WithEvents lableDesc As MSForms.Label
Public WithEvents lableSomething As MSForms.Label
Public optControl As MSForms.OptionButton

Private Sub lableDesc_Click()
   optControl.Value = True
End Sub

Private Sub lableName_Click()
   optControl.Value = True
End Sub

Private Sub lableSomething_Click()
   optControl.Value = True
End Sub

and then your form code just needs to assign the four control to each class instance.
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

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