Results 1 to 10 of 10

Thread: how to allow only letter and ucase

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,580

    how to allow only letter and ucase

    I need to permit only a letter in textbox and set ucase letter in press key event...

  2. #2
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,872

    Re: how to allow only letter and ucase

    In the KeyPress event check for the valid characters if not valid set the KeyAscii value to 0

  3. #3
    Hyperactive Member
    Join Date
    Jan 2018
    Posts
    264

    Re: how to allow only letter and ucase

    This worked for the capitalizations I cared about, you'll have to check whether it covers your keyboard setup:
    Code:
        Select Case KeyAscii
        Case 97 To 122, 224 To 254
            KeyAscii= KeyAscii - 32
        Case 135 ' ç
            KeyAscii= 128 ' Ç
        Case 65 To 90, 192 To 222, vbKeyDelete , vbKeyBack
            KeyAscii = KeyAscii
        Case Else
            KeyAscii= 0
        End Select
    edit: added backspace and delete to the legal keys
    Last edited by ahenry; Apr 4th, 2022 at 03:49 PM.

  4. #4
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,652

    Re: how to allow only letter and ucase

    If you're into undocumented APIs I just put out a demo about this...

    Code:
    Dim tli As LIMITINPUTSTRUCT
    tli.cbSize = Len(tli)
    tli.dwMask = LIM_FILTER Or LIM_FLAGS
    tli.dwFlags = LIF_CATEGORYFILTER Or LIF_WARNINGOFF Or LIF_FORCEUPPERCASE
    tli.pszFilter = LICF_ALPHA
    
    SHLimitInputEditWithFlags Text1.hWnd, tli
    That will limit input to alpha characters, automatically convert them to uppercase, and if you paste, pastes only the letters, and converts them to upper case. One more line you could add a tooltip when someone types an unallowed characted.

    Code:
    Dim tli As LIMITINPUTSTRUCT
    tli.cbSize = Len(tli)
    tli.dwMask = LIM_FILTER Or LIM_FLAGS
    tli.dwFlags = LIF_CATEGORYFILTER Or LIF_HIDETIPONVALID Or LIF_FORCEUPPERCASE
    tli.pszFilter = LICF_ALPHA
    tli.pszMessage = StrPtr("Only letters allowed")
    
    
    SHLimitInputEditWithFlags Text1.hWnd, tli
    (the module with all the declares is in the demo download)

  5. #5
    Hyperactive Member
    Join Date
    Jan 2018
    Posts
    264

    Re: how to allow only letter and ucase

    Very nice option. I didn't have a solution for paste, other than adding more code to process the validate event.

    The code I posted above also fails to process the vbKeyDelete and vbKeyBack keys and the Ctrl-A/Z/X/C/V combinations.
    Last edited by ahenry; Apr 4th, 2022 at 05:03 PM.

  6. #6
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: how to allow only letter and ucase

    add this module

    Code:
    Option Explicit
    
    Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
    
    
    Private Const ES_UPPERCASE As Long = &H8&
    Private Const ES_NUMBER As Long = &H2000&
    Private Const GWL_STYLE = (-16)
    
    
    
    
    Public Sub SetTextBoxUppercase(nTxt As Control)
        SetWindowLong nTxt.hWnd, GWL_STYLE, GetWindowLong(nTxt.hWnd, GWL_STYLE) Or ES_UPPERCASE
    End Sub
    
    
    Public Sub SetTextBoxNumeric(nTxt As Control)
        SetWindowLong nTxt.hWnd, GWL_STYLE, GetWindowLong(nTxt.hWnd, GWL_STYLE) Or ES_NUMBER
    End Sub
    Takes care of the UpperCase (even in paste).
    Restricting numeric entries example is shown above.
    Sam I am (as well as Confused at times).

  7. #7
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,652

    Re: how to allow only letter and ucase

    The ES_NUMBER style allows *only* numbers to be entered, it doesn't exclude them (it also doesn't restrict pasting non-numbers).

  8. #8
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: how to allow only letter and ucase

    Quote Originally Posted by fafalone View Post
    The ES_NUMBER style allows *only* numbers to be entered, it doesn't exclude them (it also doesn't restrict pasting non-numbers).

    I know....that's why I mentioned the numeric exclusion recommendation from others above.
    Sam I am (as well as Confused at times).

  9. #9
    Addicted Member
    Join Date
    Mar 2009
    Posts
    244

    Re: how to allow only letter and ucase

    fafalone's option is really cool, hadn't heard of that one yet.
    If you want to go the full VB6 way I'm using something like the following.

    Code:
    '##############################################################################
    '##
    '##     Function fn_IsAlpha
    '##
    '##############################################################################
    Public Function fn_IsAlpha(ByVal sText As String) As Boolean
      Dim bResult As Boolean
      Dim lIndex  As Long
      '--------------------------------------------------------------
      'If no characters we don't even have to check any further
      bResult = (Len(sText) > 0)
      If bResult Then
        For lIndex = 1 To Len(sText)
          Select Case Asc(Mid$(sText, lIndex, 1))
            Case 65 To 90:    'A-Z
            Case 97 To 122:   'a-z
            'Case 128:         'E   '€ (euro)     is not really a diacritic
            'Case 131:         'f   'ƒ (florijn)  is not really a diacritic
            Case 138:         'S   'Š
            Case 140:         'OE  'Œ
            Case 142:         'Z   'ž
            Case 153:         'TM  '™
            Case 154:         's   'š
            Case 156:         'oe  'œ
            Case 158:         'z   'ž
            Case 159:         'Y   'Ÿ
            'Case 162:         'c   '¢            is not really a diacritic
            Case 181:         'u   'µ             is not really a diacritic
            Case 192 To 197:  'A   'ÀÁÂÃÄÅ
            Case 198:         'AE  'Æ
            Case 199:         'C   'Ç
            Case 208:         'D   'Ð
            Case 200 To 203:  'E   'ÈÉÊË
            Case 204 To 207:  'I   'ÌÍÎÏ
            Case 209:         'N   'Ñ
            Case 210 To 214:  'O   'ÒÓÔÕÖ
            'Case 215:         'x   '×            is not really a diacritic
            Case 216:         'O   'Ø
            Case 217 To 220:  'U   'ÙÚÛÜ
            Case 221:         'Y   'Ý
            Case 222:         'TH  'Þ
            Case 223:         'ss  'ß
            Case 224 To 229:  'a   'àáâãäå
            Case 230:         'ae  'æ
            Case 231:         'c   'ç
            Case 232 To 235:  'e   'èéêë
            Case 236 To 239:  'i   'ìíîï
            Case 240:         'd   'ð
            Case 241:         'n   'ñ
            Case 242 To 246:  'o   'òóôõö
            Case 248:         'o   'ø
            Case 249 To 252:  'u   'ùúûü
            Case 253, 255:    'y   'ýÿ
            Case 254:         'th  'þ
            Case Else:        bResult = False
          End Select
          If Not bResult Then Exit For
        Next lIndex
      End If 'If bResult
      fn_IsAlpha = bResult
    End Function
    
    '##############################################################################
    '##
    '##     Function fnStringAlphaUpperCase
    '##
    '##############################################################################
    Private Function fnStringAlphaUpperCase(ByVal sText As String) As String
      Dim lIndex  As Long
      Dim sChar   As String
      Dim sResult As String
      sResult = ""
      sText = UCase(sText)
      For lIndex = 1 To Len(sText)
        sChar = Mid(sText, lIndex, 1)
        If fn_IsAlpha(sChar) Then sResult = sResult & sChar
      Next lIndex
      fn_StringAlphaUpperCase = sResult
    End Function
    
    '##############################################################################
    '##
    '##     Sub txt
    '##
    '##############################################################################
    Private Sub txt_Change()
      Dim lLength       As Long
      Dim lSelLength    As Long
      Dim lSelStart     As Long
      Dim sText         As String
      Static bChanging  As Boolean
      '--------------------------------------------------------------
      'Change not executed due to _Change itself?
      If Not bChanging Then
        '--------------------------------------------------------------
        'Get current settings
        sText = txt.Text
        lSelStart = txt.SelStart
        lSelLength = txt.SelLength
        '--------------------------------------------------------------
        'Change Text to what is allowed
        sText = fnStringAlphaUpperCase(sText)
        '--------------------------------------------------------------
        'Only update Textbox if text has changed
        If StrComp(txt.Text, sText) <> 0 Then
          '--------------------------------------------------------------
          'Change SelStart and SelLength?
          lLength = Len(sText)
          If lSelStart > lLength Then lSelStart = lLength
          If lSelStart + lSelLength > lLength Then lSelLength = lLength - lSelStart
          '--------------------------------------------------------------
          'Change textbox
          bChanging = True
          txt.Text = sText
          txt.SelStart = lSelStart
          txt.SelLength = lSelLength
          bChanging = False
        End If
      End If
    End Sub
    fn_IsAlpha is based on a routine we use to convert diacretics to ASCII characters, that's why all the cases instead of just combining more or all (which ofcourse has some room for better optimization, or even better localization)
    txt_Change I ripped the implementation from a class we normally call where we just pass the textbox, and it also handles some other components other than the TextBox so I'm not really sure if the changing of lStart/lSelLength is necessary for the TextBox. Also bChanging is normally a formwide property of our FormExtender, Extender.Events, as we also don't want to trigger it when we set the values on opening the form. normally our txt_Change would look like this if we want to only allow phonenumber characters:
    Code:
    Private m_cTextHandling As clsTextHandling
    WithEvents Extender As clsFormExtender
    
    Private Sub txt_Change()
      If Extender.Events Then call m_cTextHandling.Change(txt, thctPhonenumber, ext:=Extender)
    End Sub

  10. #10
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,652

    Re: how to allow only letter and ucase

    You'd have a hard time hearing of the method I posted before when I was the first person to ever detail how to use the API publicly, and did so recently

    If you search the API name, all you get is a few pages dumping the name of every shell32 export, the Windows source code leak, and my post.

    (A much, much more limited version, SHLimitInputEdit, is documented, but it's a weird one where you have to implement an interface of IItemNameLimits, and then can only supply a list of allowed xor forbidden characters, a maximum length, and nothing else; it just calls the more versatile function with those options).

Posting Permissions

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



Click Here to Expand Forum to Full Width