Sharing Events

Several Controls One Event

This is a simple example that illustrates how you can use a class module to use the same event to response to several events.
This class module contains an event that clears a textbox when it is double clicked.
Not all userform control events are available from inside a class module.

UserForm Class Module

Private colTextBoxes As New Collection 

Private Sub UserForm_Initialize()
   Dim objcontrol As MSForms.Control
   Dim objControlEvent As CControlEvents

   For Each objcontrol In Me.Controls
      If TypeOf objcontrol Is MSForms.TextBox Then
         Set objControlEvent = New CControlEvents
         Set objControlEvent.TextBox = objcontrol
         Set objControlEvent.UserForm = Me
         colTextBoxes.Add objControlEvent
      End If
   Next objcontrol
End Sub

CControlEvents Class

Public WithEvents TextBox As MSForms.TextBox 
Public UserForm As MSForms.UserForm

Private Sub TextBox_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
   Me.TextBox.Text = ""
End Sub

© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext