PDA

View Full Version : Using a class module for several userforms, identify which userform is active



Kapela2017
01-21-2023, 11:41 AM
Greetings gentlemen, I found this code, and the truth is that it works very well, it is a class module that creates a matrix for different controls (textboxes, combobox, listbox, etc.) it is really very versatile since you can assign different events For each control, I would like to use it with several userforms, for this I would need to verify which userform is loaded or active when the event is activated, I imagine it is with a function, but this must be at the class module level, thanks for any ideas you can contribute. ..



'Inside the class module

Private m_PassedControl As MSForms.Control
Private WithEvents chk As MSForms.CheckBox
Private WithEvents cbo As MSForms.ComboBox
Private WithEvents lst As MSForms.ListBox
Private WithEvents opt As MSForms.OptionButton
Private WithEvents spn As MSForms.SpinButton
Private WithEvents txt As MSForms.TextBox


Property Set ctl(PassedControl As MSForms.Control)
Set m_PassedControl = PassedControl


Select Case TypeName(PassedControl)
Case "CheckBox"
Set chk = PassedControl
Case "ComboBox"
Set cbo = PassedControl
Case "ListBox"
Set lst = PassedControl
Case "OptionButton"
Set opt = PassedControl
Case "SpinButton"
Set spn = PassedControl
Case "TextBox"
Set txt = PassedControl
End Select
End Property


Private Sub cbo_Change()
PrintControlName
End Sub


Private Sub chk_Click()
PrintControlName
End Sub


Private Sub lst_Change()
PrintControlName
End Sub


Private Sub opt_Click()
PrintControlName
End Sub


Private Sub spn_Change()
PrintControlName
End Sub


Private Sub txt_Change()
PrintControlName
End Sub


Sub PrintControlName()
Debug.Print m_PassedControl.Name
End Sub






'Inside the userform
Public collControls As Collection
Private cMultipleControls As clsMultipleControls


Private Sub UserForm_Activate()
Dim ctl As MSForms.Control


Set collControls = New Collection
For Each ctl In Me.Controls
Set cMultipleControls = New clsMultipleControls
Set cMultipleControls.ctl = ctl
collControls.Add cMultipleControls
Next ctl
End Sub

Paul_Hossler
01-21-2023, 01:03 PM
Not sure where you want to go with this but .Parent will give you the control's parent, which in MOST cases will be the UserForm.

You might have to get the grand .Parent depending but the process is the same



Option Explicit


'Inside the class module


Private m_PassedControl As MSForms.Control


Private WithEvents chk As MSForms.CheckBox
Private WithEvents cbo As MSForms.ComboBox
Private WithEvents lst As MSForms.ListBox
Private WithEvents opt As MSForms.OptionButton
Private WithEvents spn As MSForms.SpinButton
Private WithEvents txt As MSForms.TextBox




Property Get frmName() As String
frmName = m_PassedControl.Parent.Name
End Property




Property Set ctl(PassedControl As MSForms.Control)
Set m_PassedControl = PassedControl


Select Case TypeName(PassedControl)
Case "CheckBox"
Set chk = PassedControl
Case "ComboBox"
Set cbo = PassedControl
Case "ListBox"
Set lst = PassedControl
Case "OptionButton"
Set opt = PassedControl
Case "SpinButton"
Set spn = PassedControl
Case "TextBox"
Set txt = PassedControl
End Select
End Property




Private Sub cbo_Change()
PrintControlName
End Sub




Private Sub chk_Click()
PrintControlName
End Sub




Private Sub lst_Change()
PrintControlName
End Sub




Private Sub opt_Click()
PrintControlName
End Sub




Private Sub spn_Change()
PrintControlName
End Sub




Private Sub txt_Change()
PrintControlName
End Sub




Sub PrintControlName()
Select Case frmName
Case "UserForm1"
Debug.Print "Hello from UserForm1 --- ", m_PassedControl.Name
Case "UserForm2"
Debug.Print "Goodbye from UserForm2 --- ", m_PassedControl.Name
End Select
End Sub






Hello from UserForm1 --- CheckBox2
Hello from UserForm1 --- CheckBox1
Goodbye from UserForm2 --- OptionButton1
Goodbye from UserForm2 --- CheckBox1
Goodbye from UserForm2 --- CheckBox1



HOWEVER, I personally don't think it's a good OOP idea to include such specific logic in an object. I'd look for a way to keep the object non-specific and have the calling procedure handle specific tasks

SamT
01-22-2023, 04:31 AM
Since each Control in the UserForm Instantiates a new Class O)bject, Add to the Class Modue

Private PF As Object

Property Set ParentForm(Parent As Object)
Set PF = Parent
End Property

In the userForm, "Set Controls" section Add the line

cMultipleControls.ParentForm = Me

Thereafter, I presume in the Class Module Code, If a Control needs to know its UserForm

MyUserForm = Me.PF.Name
Or it can directly access the UserForm with

Me.PF(.Some Form Property)

Artik
01-22-2023, 05:16 AM
cross-post: https://www.excelforum.com/excel-programming-vba-macros/1398110-using-a-class-module-for-several-userforms-identify-which-userform-is-active.html

Artik

Paul_Hossler
01-22-2023, 09:25 AM
This is what confused me from the OP.


I would like to use it with several userforms, for this I would need to verify which userform is loaded or active when the event is activated, I imagine it is with a function, but this must be at the class module level,

Aussiebear
01-22-2023, 01:43 PM
Since this is not the first time that kapela2017 has been notified about cross posting here on this forum...... this thread is now closed. Kapela2017 needs to find a new home.