Hello losmi8,
To answer the second part of the your question, there is no built in method you can apply to all the TextBoxes. However, it is possible to create a common event handler that is shared among all the TextBox controls.
There are 3 parts to doing this. There are 2 Class modules. One module is for the TextBox event handling. The second creates a collection of individual event handlers and set references to each individual TextBox. Lastly, you need to add code to the form to add the TextBoxes to this collection.
NOTE: Each Class Module must be separate. The code can not be combined into a single module.
After adding the Class module, change the name in the Properties dialog to match the name in the header above the code. If you do not rename the modules, the code will not work.
TextBoxEvents
'Written: January 31, 2011
'Author: Leith Ross
'Summary: This Class Module handles the events for ActiveX TextBox objects.
'
'NOTE: This Class is used with the TextBoxObjects Class.
Public WithEvents EventObject As MSForms.TextBox
Private Sub EventObject_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
With EventObject
.SelStart = 0
.SelLength = Len(.Value)
End With
End Sub
TextBoxObjects
'Written: January 31, 2011
'Author: Leith Ross
'Summary: This Class Module is a collection of ActiveX TextBox objects and
' their event objects (TextBoxEvents).
'
'NOTE: This Class is used with the TextBoxEvents Class.
Dim TxtBox As Object
Dim TextBoxHandlers As New Collection
Dim TextBoxObjects As New Collection
Public Function Add(ByRef Text_Box As Object) As Object
'Create an event handler object
Set TxtBox = New TextBoxEvents
'Attach the event handler object to an existing TextBox
Set TxtBox.EventObject = Text_Box
'Save the event handler and the TextBox in their respective Collections
TextBoxHandlers.Add TxtBox
TextBoxObjects.Add Text_Box
'Return a reference to the Text_Box object
Set Add = Text_Box
End Function
Public Function Count() As Long
Count = TextBoxObjects.Count
End Function
Public Function Object(ByVal lngIndex As Long) As Object
Set Object = TextBoxObjects.Item(lngIndex)
End Function
Sub Remove(ByVal lngIndex As Long)
TextBoxObjects.Remove lngIndex
End Sub
UserForm Code
'This Line Must Be At the Top
Public colTextBoxes As TextBoxObjects
Private Sub UserForm_Initialize()
Set colTextBoxes = New TextBoxObjects
For I = 0 To Controls.Count - 1
If TypeName(Controls(I)) = "TextBox" Then
colTextBoxes.Add Me.Controls(I)
End If
Next I
End Sub
Bookmarks