Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
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 |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Event Code for Controls created at runtime... | Excel Programming | |||
Assigning events to runtime-created controls - is it possible? | Excel Discussion (Misc queries) | |||
add event to controls added in runtime | Excel Programming | |||
Event procedures for controls added with Controls.Add | Excel Programming | |||
How to create controls at runtime | Excel Programming |