powershell - Limiting text box entry to numbers or numpad only - no special characters -
i have small program accepts integer , converts datetime
. however, try use keycode
allow numbers both keyboard , numpad, , eliminate special characters , letters. code allowing shift+num enter special characters. how eliminate them being entered?
$fromdatetext.add_keydown({keydown}) $todatetext.add_keydown({keydown}) #textbox function keydown() { if ($fromdatetext.focused -eq $true -or $todatetext.focused -eq $true) { if ($_.keycode -gt 47 -and $_.keycode -lt 58 -or $_.keycode -gt 95 -and $_.keycode -lt 106 -or $_.keycode -eq 8) { $_.suppresskeypress = $false } else { $_.suppresskeypress = $true } } }
instead of intercepting keydown
event, i'd remove non-digit character textbox
every time textchanged
event raised:
$todatetext.add_textchanged({ # check if text contains non-digits if($tbox.text -match '\d'){ # if so, remove them $tbox.text = $tbox.text -replace '\d' # if text still has value, move cursor end of number if($tbox.text.length -gt 0){ $tbox.focus() $tbox.selectionstart = $tbox.text.length } } })
way easier trying infer input value key event args
Comments
Post a Comment