Subclass a TextBox Control Using WithEvents

Instead of adding the same code over and over to all of the textbox controls on one or more forms that you want to act the same, subclass the textbox. This allows you to use a technique call Multicasting (see source) so that you can create a single textbox object that consolidates your code into one location. Then you can create as many instances of the textbox control that you need and all of them will inherit the same behavior.

This example uses 2 textbox controls. Both only allow uppercase letters, highlight their text when they get focus, trim their contents when they lose focus and populate the third textbox when their change event triggers. The code to do this is located in a single class module instead of being duplicated in both textboxes. The key to this is the WithEvents statement.

Sub-Class TextBox using WithEvents to provide Multicasting capabilities
Download Source Code

Class Module Code

Create a Class Module named AlphaTextBox and Copy the following code to it:

   Option Explicit
   '
   ' All form1 textboxes are of this type. That way the code for any textbox event can be
   ' located in one central place (versus the same code in every GotFocus, ... event).
   '
   ' By using the "WithEvents" keyword, all events that occur
   ' within the textboxes are passed to this class where they can be handled or not.
   '
   Public WithEvents TextBox As TextBox

   Private Sub TextBox_Change()
      '
      ' Echo back what the user entered. Since the textbox is an
      ' object, we must reference the appropriate properties.
      '
      With Form1
         .lblName = .txtFirst.TextBox.Text & " " & 
         .txtLast.TextBox.Text
      End With
   End Sub

   Private Sub TextBox_GotFocus()
      '
      ' Highlight the text.
      '
      With TextBox
         .SelStart = 0
         .SelLength = Len(.Text)
      End With
   End Sub

   Private Sub TextBox_KeyPress(KeyAscii As Integer)

      Select Case KeyAscii
      Case 8 To 10, 13, 27 'Control characters<
         KeyAscii = KeyAscii
      Case 65 To 90 'Upper case letters
         KeyAscii = KeyAscii
      Case 97 To 122 'Lower case letters
         KeyAscii = Asc(UCase$(Chr$(KeyAscii)))
      Case Else 'Discard anything else
         KeyAscii = 0
      End Select
   End Sub

   Private Sub TextBox_LostFocus()
     TextBox.Text = Trim$(TextBox.Text)
   End Sub

Form Code

Copy the following code to form1:

   Option Explicit
   '
   ' All textboxes on form1 are from Class AlphaTextBox
   ' so they all handle events the same. This is know as
   ' Multicasting - the ability of objects to notify all
   ' other objects that something has occurred.
   '
   ' Create an instance of the AlphaTextbox class
   ' for each textbox used.
   '
   Public txtFirst As New AlphaTextBox
   Public txtLast As New AlphaTextBox

   Private Sub Form_Load()
      '
      ' Assign our textbox controls to the textbox
      ' property of the AlphaTextBox objects. This way,
      ' all events are passed to the AlphaTextBox class.
      '
      Set txtFirst.TextBox = txtStdFirst
      Set txtLast.TextBox = txtStdLast
   End Sub

Instructions

Run the source code using the debugger. It is more informative to step through the code so you can see how the class module code is invoked due to the WithEvents statement. Type in your first and last name and watch the control flow of the program.




About TheScarms
About TheScarms


Sample code
version info

If you use this code, please mention "www.TheScarms.com"

Email this page


© Copyright 2024 TheScarms
Goto top of page