Jaafar Tribak
Well-known Member
- Joined
- Dec 5, 2002
- Messages
- 9,779
- Office Version
- 2016
- Platform
- Windows
Greetings all.
Let me see if I can explain this clearly.
Suppose I have a UserForm with 10 CommandButtons. Each CommandButton has the same code inside its _Click event handler.
Now, because the Click event code for each CommandButton are the same ,we can, as you know, use a small Class module to define a generic _Click event handler for all the CommandButtons and then upon initializing the UserForm we can create 10 instances of the Class Module and assign each CommandButton to a Public variable declared inside the Class module via the Keyword WithEvents .
Example :
1- Code in the Class module ( Class named cmbClass )
2- Code in the UserForm module :
Now my question is : How can we create a generic control event WITHOUT having to add a seperate Class module hence keeping the entire code encapsulated inside the UserForm module ?
My initial thought was to declare the WithEvents variable inside the UserForm Module (Given the fact that the UserForm module is itself a Class module ) to avoid the need of a separate Class and to keep the entire code within the userform.
So I was thinking to declare the variable WithEvents as an array, something like :
Private WithEvents CmbGroup() As MSForms.CommandButton
to store a reference to each of the 10 CommandButtons but obviously that didn't work.
Another idea i had was to instantiate 10 userforms (1 for each CommandButton) in the UserForm_Initialize event but obviously that would be crazy as that would create an infinite loop !
Any thoughts ?
Regards.
EDIT : Sorry I forgot to mention that i don't want to declare 10 seperate WithEvents variables at the top of the userform module one for each CommandButton which i know will work as that would make the code cumbersome to write ( Imagine having dozens of commandbuttons ! ) I am actually looking for a more elegant solution.
Let me see if I can explain this clearly.
Suppose I have a UserForm with 10 CommandButtons. Each CommandButton has the same code inside its _Click event handler.
Now, because the Click event code for each CommandButton are the same ,we can, as you know, use a small Class module to define a generic _Click event handler for all the CommandButtons and then upon initializing the UserForm we can create 10 instances of the Class Module and assign each CommandButton to a Public variable declared inside the Class module via the Keyword WithEvents .
Example :
1- Code in the Class module ( Class named cmbClass )
Code:
Public WithEvents CmbGroup As MSForms.CommandButton
Private Sub CmbGroup_Click()
MsgBox CmbGroup.Name
End Sub
2- Code in the UserForm module :
Code:
Option Base 1
Dim cmbClassArray() As cmbClass
Private Sub UserForm_Initialize()
Dim cmbClassInstance As cmbClass
Dim oCtl As Control
Dim i As Long
For Each oCtl In Me.Controls
If TypeOf oCtl Is CommandButton Then
Set cmbClassInstance = New cmbClass
Set cmbClassInstance.CmbGroup = oCtl
i = i + 1
ReDim Preserve cmbClassArray(i)
Set cmbClassArray(i) = cmbClassInstance
End If
Next
Set oCtl = Nothing
Set cmbClassInstance = Nothing
End Sub
Now my question is : How can we create a generic control event WITHOUT having to add a seperate Class module hence keeping the entire code encapsulated inside the UserForm module ?
My initial thought was to declare the WithEvents variable inside the UserForm Module (Given the fact that the UserForm module is itself a Class module ) to avoid the need of a separate Class and to keep the entire code within the userform.
So I was thinking to declare the variable WithEvents as an array, something like :
Private WithEvents CmbGroup() As MSForms.CommandButton
to store a reference to each of the 10 CommandButtons but obviously that didn't work.
Another idea i had was to instantiate 10 userforms (1 for each CommandButton) in the UserForm_Initialize event but obviously that would be crazy as that would create an infinite loop !
Any thoughts ?
Regards.
EDIT : Sorry I forgot to mention that i don't want to declare 10 seperate WithEvents variables at the top of the userform module one for each CommandButton which i know will work as that would make the code cumbersome to write ( Imagine having dozens of commandbuttons ! ) I am actually looking for a more elegant solution.
Last edited: