-
Apr 4th, 2022, 02:09 PM
#1
Thread Starter
PowerPoster
how to allow only letter and ucase
I need to permit only a letter in textbox and set ucase letter in press key event...
-
Apr 4th, 2022, 02:12 PM
#2
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
-
Apr 4th, 2022, 02:40 PM
#3
Hyperactive Member
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.
-
Apr 4th, 2022, 02:44 PM
#4
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)
-
Apr 4th, 2022, 03:43 PM
#5
Hyperactive Member
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.
-
Apr 5th, 2022, 06:28 AM
#6
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).
-
Apr 5th, 2022, 06:05 PM
#7
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).
-
Apr 5th, 2022, 07:15 PM
#8
Re: how to allow only letter and ucase
Originally Posted by fafalone
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).
-
Apr 6th, 2022, 05:15 AM
#9
Addicted Member
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
-
Apr 6th, 2022, 06:12 AM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|