How to filter text input on Unicode fields?

ruimac

Member
Lets say that I want a text field to only receive numerical input.
I know I can intercept the key codes with the OnKeyDown and OnKeyPress events.
But, that only allows me to check for the code of the key.
So, how can I filter out all key codes that are not numerical and above ASCII 31 (to allow for backspace and Return) and only allow the Unicode field to receive numerical inputs?
 
No sure what you intend to do but if you need a only numerical component why not use the DragEdit component?
 

Attachments

  • Screenshot 2023-11-23 121843.png
    Screenshot 2023-11-23 121843.png
    21.2 KB · Views: 5
No sure what you intend to do but if you need a only numerical component why not use the DragEdit component?
I need a multi-line text field, where the user can enter a list of frames, something like:

53
75
93
125
160

So, I need to be able to filter the keys that the field accepts so that only numbers, delete and return are allowed.
 
Ok,
Make a Unicode memo and on the OnKeyPress event place this code:
Code:
Dim WshShell
Set WshShell = CreateObject("WScript.Shell")

Sub TWUniMemo1KeyPress(Sender, Key)
    if key <> 13 and key <> 8 and (key < 48 or key > 58) then
        WshShell.SendKeys "{BACKSPACE}"
    end if
End sub

This will allow only numbers, backspace or return to be typed in the memo
 
This code should also work:
Code:
Sub TWUniMemo1KeyPress(Sender, Key)
    If Not (Key >= 48 And Key <= 57 Or Key = 8 Or Key = 13) Then
       Key = 0
    End If
End sub

J.
 
Back
Top