View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
joel joel is offline
external usenet poster
 
Posts: 9,101
Default Class Raise Event not firing

Each combox need its own click function. this function can be only one line
which calls a common routiune

Private Sub cls_combobox1_click()
n = "1"
call cls_GetClickedControl(n)
End Sub
Private Sub cls_combobox2_click()
n = "2"
call cls_GetClickedControl(n)
End Sub
Private Sub cls_combobox3_click()
n = "3"
call cls_GetClickedControl(n)
End Sub
Private Sub cls_combobox4_click()
n = "4"
call cls_GetClickedControl(n)
End Sub
Private Sub cls_combobox5_click()
n = "5"
call cls_GetClickedControl(n)
End Sub
Private Sub cls_combobox8_click()
n = "6"
call cls_GetClickedControl(n)
End Sub

Private Sub cls_GetClickedControl(n As String)
MsgBox n 'Only fires on last combobox click
End Sub

"Phillip" wrote:

I have 6 Combobox controls on a userform
named ComboBox1 to Combobox6

When I click on a combobox the class
Msgbox displays the combobox name
I clicked corrrectly.

However the raise event only fires on
the last combobox

It seems to be a scope problem but I
cannot see the answer

What extra code do I need to fire
the raise event for any combobox I click


UserForm Code

Public colevents As Collection
Public WithEvents cls As ClsComboEvent

Private Sub cls_GetClickedControl(n As String)
MsgBox n 'Only fires on last combobox click
End Sub


Private Sub UserForm_Initialize()
Dim ctrl As MSForms.Control
Dim x As Long
Set colevents = New Collection
For Each ctrl In Me.Controls
If TypeOf ctrl Is ComboBox Then
For x = 1 To 3 'add 3 choices
ctrl.AddItem "Choice" & CStr(x)
Next
Set cls = New ClsComboEvent
Set cls.Cevent = ctrl
colevents.Add cls
End If
Next
End Sub

Class Module code

Class is named cls

Public WithEvents Cevent As MSForms.ComboBox
Event GetClickedControl(n As String)

Private Sub Cevent_Click()
Dim ControlName As String
ControlName = Cevent.Name
'MsgBox ControlName 'this fires correctly
RaiseEvent GetClickedControl(ControlName)
End Sub