Results 1 to 5 of 5

Thread: Subclassing TextBox1

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Senior Member
    Join Date
    Oct 2011
    Posts
    135
    Rep Power
    14

    Subclassing TextBox1

    Hi,

    With Excel 2003 you can manage Subclassing a TextBox in UserForm?

    Seeking advice, info on the method
    Thanks in advance

  2. #2
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,402
    Rep Power
    10
    Max, you'll have to give more details. What do you mean by SubClassing?
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

  3. #3
    Senior Member
    Join Date
    Oct 2011
    Posts
    135
    Rep Power
    14
    Hi,

    Seeking information on this method

    I opened the following page: Excel - Cool Worksheet Keydown Event (like The One For Textboxes) & W/out Subclassing - Hi all I have... - Free Excel Help

    I was wondering if you could use this technique to manage a Textbox1 in Userform1

    So you can handle numbers or letters

  4. #4
    Forum Guru Rick Rothstein's Avatar
    Join Date
    Feb 2012
    Posts
    662
    Rep Power
    14
    Quote Originally Posted by PcMax View Post
    I opened the following page: Excel - Cool Worksheet Keydown Event (like The One For Textboxes) & W/out Subclassing - Hi all I have... - Free Excel Help

    I was wondering if you could use this technique to manage a Textbox1 in Userform1

    So you can handle numbers or letters
    This is a perfect example of why it is a good idea to tell us what you are trying to do along with the specific question you want to ask... you do not need to subclass the TextBox (not sure if you even can) in order to restrict its input to, say, only digits. Below are 3 different event code procedures for you to consider. The first one restricts entry to a TextBox to only digits, the second one restricts entry to a TextBox to floating point numbers (be sure to read the comments at the beginning of the code) and the last one restricts entry to upper or lower case letters only (no digits or special characters allowed). These routines not only perform their filtering operation on typed in characters, but also on text attempted to be pasted into the TextBox as well.

    Code:
    '  Accept Digits Only
    
    Dim LastPosition As Long
    
    Private Sub TextBox1_Change()
      Static LastText As String
      Static SecondTime As Boolean
      If Not SecondTime Then
        With TextBox1
          If .Text Like "*[!0-9]*" Then
            Beep
            SecondTime = True
            .Text = LastText
            .SelStart = LastPosition
          Else
            LastText = .Text
          End If
        End With
      End If
      SecondTime = False
    End Sub
    
    Private Sub TextBox1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
      With TextBox1
        LastPosition = .SelStart
        'Place any other MouseDown event code here
      End With
    End Sub
    
    Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
      With TextBox1
        LastPosition = .SelStart
        'Place any other KeyPress checking code here
      End With
    End Sub
    Code:
    '  Accept floating point numbers only
    
    '  Set the maximum number of digits before the decimal point in the MaxWhole constant.
    '  Set the maximum number of digits after the decimal point in the MaxDecimal constant.
    '  Use any large number for each of these settings if you do not need to retrict the size of the entry.
    
    Dim LastPosition As Long
    
    Private Sub TextBox1_Change()
      Static LastText As String
      Static SecondTime As Boolean
      If Not SecondTime Then
        With TextBox1
          If Not SecondTime Then
            If .Text Like "*[!0-9.]*" Or .Text Like "*.*.*" Or .Text Like "*." & String$(1 + _
                        MaxDecimal, "#") Or .Text Like String$(MaxWhole, "#") & "[!.]*" Then
              Beep
              SecondTime = True
              .Text = LastText
              .SelStart = LastPosition
            Else
              LastText = .Text
            End If
          End If
        End With
      End If
      SecondTime = False
    End Sub
    
    Private Sub TextBox1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
      With TextBox1
        LastPosition = .SelStart
        'Place any other MouseDown event code here
      End With
    End Sub
    
    Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
      With TextBox1
        LastPosition = .SelStart
        'Place any other KeyPress checking code here
      End With
    End Sub
    Code:
    '  Accept upper/lower case letters only
    
    Dim LastPosition As Long
    
    Private Sub TextBox1_Change()
      Static LastText As String
      Static SecondTime As Boolean
      If Not SecondTime Then
        With TextBox1
         If .Text Like "*[!A-Za-z]*" Then
            Beep
            SecondTime = True
            .Text = LastText
            .SelStart = LastPosition
          Else
            LastText = .Text
          End If
        End With
      End If
      SecondTime = False
    End Sub
    
    Private Sub TextBox1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
      With TextBox1
        LastPosition = .SelStart
        'Place any other MouseDown event code here
      End With
    End Sub
    
    Private Sub TextBox1_KeyPress(KeyAscii As Integer)
      With TextBox1
        LastPosition = .SelStart
        'Place any other KeyPress checking code here
      End With
    End Sub
    Last edited by Rick Rothstein; 02-23-2012 at 01:25 AM.

  5. #5
    Senior Member
    Join Date
    Oct 2011
    Posts
    135
    Rep Power
    14
    Hi,

    Meanwhile, I thank the provided code to filter the data in textbox.

    In this way, if textbox insert more I enter the code you need in every object.
    My research of subclassing would delete this setting.
    From the first tests I did, I noticed an instability Subclassing using the code modified to userform.
    That's why information on the method.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •