
'1)
===================== questo è nel modulo della userform
Public Sub HandleEvent(ByVal Evento As clsEvent, Sender As Object)
Select Case Evento.eEvType
Case eCBchange
MsgBox "Il valore di " & Sender.cmd.Name & "è " & Evento.eValue
End Select
End Sub
2)
===================== e questo nel modulo di classe del controllo
Private Sub cmd_Change()
cEvent.eValue = cmd.Value
UserForm1.HandleEvent cEvent, Me
End Sub
|
'---------------------------------
'nel modulo di classe clsEvent
'---------------------------------
Public Enum tEvent
eNoEvent = 0
eTBchange = 1
evMouseClick = 2
eCBchange = 4
End Enum
Private eValue_ As Variant
Private eEvType_ As tEvent
Private eParent_ As MSForms.UserForm
'
Property Get eParent() As MSForms.UserForm
Set eParent = eParent_
End Property
Property Let eParent(Papa As MSForms.UserForm)
Set eParent_ = Papa
End Property
Property Get eValue() As Variant
eValue = eValue_
End Property
Property Let eValue(Value As Variant)
eValue_ = Value
End Property
Property Get eEvType() As tEvent
eEvType = eEvType_
End Property
Property Let eEvType(evtype As tEvent)
eEvType_ = evtype
End Property
'---------------------------------
'nel modulo di classe clsCHKB
'---------------------------------
Public WithEvents cmd As MSForms.CheckBox
Private cEvent As clsEvent
Private Sub Class_Initialize()
Set cEvent = New clsEvent
cEvent.eEvType = eCBchange
End Sub
Private Sub Class_Terminate()
Set cEvent = Nothing
End Sub
Private Sub cmd_Change()
cEvent.eParent = Me.cmd.Parent
With cEvent.eParent.ActiveControl
MsgBox "Il valore del controllo " & .Name & " è " & .Value
End With
End Sub
'---------------------------------
'nel modulo di classe della Userform
'---------------------------------
Dim CKB1 As clsCHKB
Dim CKB2 As clsCHKB
Private Sub UserForm_Initialize()
Set CKB1 = New clsCHKB
Set CKB1.cmd = Me.CheckBox1
Set CKB2 = New clsCHKB
Set CKB2.cmd = Me.CheckBox2
End Sub
|
**esempio 1**
Questo nel modulo della form
__________________________
Option Explicit
Public WithEvents CKB1 As clsCHKB
Private Sub CKB1_HandleEvent(ByVal Evento As clsEvent, ByVal Sender As Object)
Select Case Evento.eEvType
Case eCBchange
MsgBox "Il valore di " & Sender.cmd.Name & "è " & Evento.eValue
End Select
End Sub
Private Sub UserForm_Initialize()
Set CKB1 = New clsCHKB
Set CKB1.cmd = Me.CheckBox1
End Sub
Private Sub UserForm_Terminate()
Set CKB1 = Nothing
End Sub
___________________________________
Questo nel modulo della classe
_________________________
Option Explicit
Public Event HandleEvent(ByVal Evento As clsEvent, ByVal Sender As Object)
Public WithEvents cmd As MSForms.CheckBox
Private cEvent As clsEvent
Private Sub Class_Initialize()
Set cEvent = New clsEvent
cEvent.eEvType = eCBchange
End Sub
Private Sub Class_Terminate()
Set cEvent = Nothing
End Sub
Private Sub cmd_Change()
cEvent.eValue = cmd.Value
RaiseEvent HandleEvent(cEvent, Me)
End Sub
_______________________________________________________________
**esempio 2**
Questo nel modulo della form
__________________________
Private Sub UserForm_Initialize()
Dim ctl As MSForms.Control
Set obkColl = New Collection
For Each ctl In Me.Controls '<==== in realtà i controlli verranno creati in runtime uno a uno
If TypeOf ctl Is OptionButton Then
Set ctl = New clsCHKB
Set ctl.cmd = ctl
obkColl.Add ctl
End If
Next ctl
End Sub
|
**esempio 2**
Questo nel modulo della form
__________________________
dim obkColl as Collection
Private Sub UserForm_Initialize()
Dim ctl As MSForms.Control
Set obkColl = New Collection
For Each ctl In Me.Controls '<==== in realtà i controlli verranno creati in runtime uno a uno
If TypeOf ctl Is OptionButton Then
Set ctl = New clsCHKB
Set ctl.cmd = ctl
obkColl.Add ctl
End If
Next ctl
End Sub |
