Fredrik1987
Board Regular
- Joined
- Nov 5, 2015
- Messages
- 69
Hi!
I'm trying to assign events to the text- and comboboxes in a userform.
It works fine as long as I just add one of the control types (TextBox or ComboBox), or assign each control to a different property name.The problem occurs when I'm assigning both Textbox and ComboBox to the same in cFormEvents (ControlProperty)
Form name = uLeggTilFerie
Module name = mFormEvents
Class module name = cFormEvents
Here's the code in mFormEvents:
Code in cFormEvents
I'm guessing the problem is my lack of knowlegde regarding properties and class modules
I'm trying to assign events to the text- and comboboxes in a userform.
It works fine as long as I just add one of the control types (TextBox or ComboBox), or assign each control to a different property name.The problem occurs when I'm assigning both Textbox and ComboBox to the same in cFormEvents (ControlProperty)
Form name = uLeggTilFerie
Module name = mFormEvents
Class module name = cFormEvents
Here's the code in mFormEvents:
Code:
Option Explicit
'/ Control and events
Public Control As MSForms.Control
Private TextBoxEvent As cFormEvents
Private ComboBoxEvent As cFormEvents
'/ Collections
Private TextBoxCollection As Collection
Private ComboBoxCollection As Collection
Sub SetProperty()
'/==================================================================================================
'/ Set Control events
'/==================================================================================================
Set TextBoxCollection = New Collection
Set ComboBoxCollection = New Collection
'/ Loop throug each control in userform and asign property to each control
For Each Control In uLeggTilFerie.Controls
'/ TextBox events
If TypeOf Control Is MSForms.TextBox Then
'/ Compile Error: Argument not optional
Set TextBoxEvent = New cFormEvents
Set TextBoxEvent.ControlProperty = Control
TextBoxCollection.Add TextBoxEvent
'/ ComboBox events
ElseIf TypeOf Control Is MSForms.ComboBox Then
Set ComboBoxEvent = New cFormEvents
Set ComboBoxEvent.ControlProperty = Control
ComboBoxCollection.Add ComboBoxEvent
End If
Next Control
Set TextBoxEvent = Nothing
Set ComboBoxEvent = Nothing
End Sub
Code in cFormEvents
Code:
Option Explicit
Private WithEvents MyTextBox As MSForms.TextBox
Private WithEvents MyComboBox As MSForms.ComboBox
Private WithEvents MyButton As MSForms.CommandButton
Public Property Set ControlProperty(TB As MSForms.TextBox, CB As MSForms.ComboBox)
Set MyTextBox = TB
Set MyComboBox = CB
End Property
I'm guessing the problem is my lack of knowlegde regarding properties and class modules
