Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 33
Default Class Raise Event not firing

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


  #2   Report Post  
Posted to microsoft.public.excel.programming
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



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,298
Default Class Raise Event not firing

here's my demo - it can be done as you wish.
I start with a new userform with 6 combo boxes. add any other controls you
wish - the code identifies any number of comboboxes, so yuo can add as many
as you want. Also, for this demo, some items are added to ecah combo box

It requires a class module. I called it clsControl
This is the code in this class module:

Option Explicit
Public WithEvents m_MyComboBox As MSForms.ComboBox
Public Event Changed(text As String)
Private Sub m_MyComboBox_Change()
MsgBox "You changed " & m_MyComboBox.Name & vbLf & _
m_MyComboBox.Value
m_MyComboBox.Tag = "True"
End Sub


This is the code behind the Userform:
Option Explicit
Dim colComBoxes As New Collection
Dim WithEvents ctComboBox As clsControl

Private Sub UserForm_Initialize()
Dim ctrl As Control
For Each ctrl In Controls
If ctrl.Name Like "Combo*" Then
AddValues ctrl 'for demo
Set ctComboBox = New clsControl
Set ctComboBox.m_MyComboBox = ctrl
colComBoxes.Add ctComboBox
End If
Next
End Sub
'the followign puts data into each combo
Private Sub AddValues(ctrl As ComboBox)
ctrl.AddItem Chr(65 + Int(Rnd() * 26))
ctrl.AddItem Chr(65 + Int(Rnd() * 26))
ctrl.AddItem Chr(65 + Int(Rnd() * 26))
End Sub

-----
shoudl be easy enough to understand.




"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



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,298
Default Class Raise Event not firing

two changes

please cjamhe the class module code to this:
Option Explicit
Public WithEvents m_MyComboBox As MSForms.ComboBox
Public Event Changed(text As String)
Private Sub m_MyComboBox_Change()
UserForm1.Changed (m_MyComboBox.Name)
End Sub

Please add this procedure to the userfor:
Sub Changed(text As String)
MsgBox text
End Sub


"Patrick Molloy" wrote:

here's my demo - it can be done as you wish.
I start with a new userform with 6 combo boxes. add any other controls you
wish - the code identifies any number of comboboxes, so yuo can add as many
as you want. Also, for this demo, some items are added to ecah combo box

It requires a class module. I called it clsControl
This is the code in this class module:

Option Explicit
Public WithEvents m_MyComboBox As MSForms.ComboBox
Public Event Changed(text As String)
Private Sub m_MyComboBox_Change()
MsgBox "You changed " & m_MyComboBox.Name & vbLf & _
m_MyComboBox.Value
m_MyComboBox.Tag = "True"
End Sub


This is the code behind the Userform:
Option Explicit
Dim colComBoxes As New Collection
Dim WithEvents ctComboBox As clsControl

Private Sub UserForm_Initialize()
Dim ctrl As Control
For Each ctrl In Controls
If ctrl.Name Like "Combo*" Then
AddValues ctrl 'for demo
Set ctComboBox = New clsControl
Set ctComboBox.m_MyComboBox = ctrl
colComBoxes.Add ctComboBox
End If
Next
End Sub
'the followign puts data into each combo
Private Sub AddValues(ctrl As ComboBox)
ctrl.AddItem Chr(65 + Int(Rnd() * 26))
ctrl.AddItem Chr(65 + Int(Rnd() * 26))
ctrl.AddItem Chr(65 + Int(Rnd() * 26))
End Sub

-----
shoudl be easy enough to understand.




"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



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Raise Event Problem Phillip[_5_] Excel Programming 4 August 5th 09 03:13 PM
Click event on menu item is lost after first time firing of the event [email protected] Excel Programming 1 April 2nd 07 01:25 PM
OnTime event not firing in Workbook_Open event procedure GingerTommy Excel Programming 0 September 24th 03 03:18 PM
Does formatting raise an event Sandy V[_2_] Excel Programming 1 September 10th 03 05:45 PM
dialog raise event? Tom Ogilvy Excel Programming 0 July 23rd 03 01:38 PM


All times are GMT +1. The time now is 09:20 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"