Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 108
Default Event Code for Controls created at runtime...

Roedd <<Neily wedi ysgrifennu:

Hi,

Is there a way of creating event code for a control that is created at
runtime?

For example, I have a form which based on a user created set of
questions, builds the form with a check box and combobox for each
questions. I can't predict how many questions the user will create.


OK. This is quite involved, but this is how I would do it:

Add a class module to your project and call it ComboSink. This will capture
the events from the ComboBox controls that you add at run time. Paste in the
following:

Option Explicit

Private WithEvents m_cboAct As MSForms.ComboBox
Private m_clnParent As ComboSinks

Private Sub Class_Terminate()
Set m_cboAct = Nothing
Set m_clnParent = Nothing
End Sub

Private Sub m_cboAct_Change()
Me.Parent.RaiseChangeEvent m_cboAct.Name
End Sub
Private Sub m_cboAct_Click()
Me.Parent.RaiseClickEvent m_cboAct.Name
End Sub
Private Sub m_cboAct_DropButtonClick()
Me.Parent.RaiseDropButtonClickEvent m_cboAct.Name
End Sub

Public Property Get ComboBox() As ComboBox
Set ComboBox = m_cboAct
End Property
Public Property Set ComboBox(Value As ComboBox)
Set m_cboAct = Value
End Property

Public Property Get Parent() As ComboSinks
Set Parent = m_clnParent
End Property
Public Property Set Parent(Value As ComboSinks)
Set m_clnParent = Value
End Property

Now you need a way of collecting all of the event sink classes together in
an organised fashion. Create another class module and call it ComboSinks.
Then paste in the following:

Option Explicit

Private m_clnComboSinks As Collection

Public Event Change(ByVal Name As String)
Public Event Click(ByVal Name As String)
Public Event DropButtonClick(ByVal Name As String)

Public Function Add(ComboBox As MSForms.ComboBox) _
As ComboSink
Dim objTemp As ComboSink
Set objTemp = New ComboSink
Set objTemp.ComboBox = ComboBox
Set objTemp.Parent = Me
m_clnComboSinks.Add objTemp, ComboBox.Name
End Function
Public Property Get Item(Value As Variant) As ComboSink
Set Item = m_clnComboSinks(Value)
End Property
Public Sub Remove(Value As Variant)
m_clnComboSinks.Remove Value
End Sub
Public Property Get Count() As Long
Count = m_clnComboSinks.Count
End Property

Friend Sub RaiseChangeEvent(Name As String)
RaiseEvent Change(Name)
End Sub
Friend Sub RaiseClickEvent(Name As String)
RaiseEvent Click(Name)
End Sub
Friend Sub RaiseDropButtonClickEvent(Name As String)
RaiseEvent DropButtonClick(Name)
End Sub

Private Sub Class_Initialize()
Set m_clnComboSinks = New Collection
End Sub

That's really all you need. To demonstrate, add a UserForm to a VBA project,
add a Command Button (keep the default name) and then paste this code into
the form's code module:

Option Explicit

Private WithEvents m_objCombos As ComboSinks

Private sglTop As Single
Private lngCounter As Long

Private Sub CommandButton1_Click()
Dim objCbo As MSForms.ComboBox
Dim i As Long

For i = 1 To 4

Set objCbo = Me.Controls.Add( _
"Forms.Combobox.1", "MyCbo" & _
(lngCounter) + i, True)
objCbo.top = sglTop
objCbo.Text = objCbo.Name
m_objCombos.Add objCbo
sglTop = sglTop + objCbo.Height

Next

lngCounter = lngCounter + 3

End Sub

Private Sub m_objCombos_Change(ByVal Name As String)
MsgBox Name & " Changed!"
End Sub
Private Sub m_objCombos_Click(ByVal Name As String)
MsgBox Name & " Clicked!"
End Sub
Private Sub m_objCombos_DropButtonClick _
(ByVal Name As String)
MsgBox Name & " DropDown Button Clicked!"
End Sub

Private Sub UserForm_Initialize()
Set m_objCombos = New ComboSinks
lngCounter = 1
End Sub

Now run the form and click the button.

As you can see, this is all done using the magic of the WithEvents
declaration.

HTH

Rob


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
Event Code for Controls created at runtime... Bob Phillips Excel Programming 0 December 6th 06 10:35 AM
Assigning events to runtime-created controls - is it possible? BizMark Excel Discussion (Misc queries) 1 November 20th 06 09:36 AM
add event to controls added in runtime Brotha lee Excel Programming 1 May 21st 05 10:32 AM
Event procedures for controls added with Controls.Add John Austin[_4_] Excel Programming 1 March 9th 05 03:31 PM
How to create controls at runtime Andy Chan Excel Programming 3 August 30th 04 10:07 AM


All times are GMT +1. The time now is 01:44 AM.

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"