Tuesday, December 9, 2008

Use KeyChar to limit the characters that can be entered into a Text Box

A method to allow the user to enter only certain specific characters into a text box and ignore any other key. In this example only numbers, the Backspace key and the period will be allowed, everything else is ignored.

'allow only numbers, the Backspace key and the period

If (e.KeyChar < "0" OrElse e.KeyChar > "9") _
AndAlso e.KeyChar <> ControlChars.Back AndAlso e.KeyChar <> "." Then
'cancel keys
e.Handled = True
End If

No comments: