I have textboxes that require only 0123456789 <decimal> <backspace> and <negative> keystrokes.

I have coded the following but when I press any key within the textbox it does not fire the event.

And what statement do I write to cancel the keystroke from executing?


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Public Sub txtFahrenheit_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtFahrenheit.KeyPress
'Only accept the following keyboard buttons
' [0123456789] ; Asc 48-57 respectively
' [<backspace>] ; Asc 8
' [<decimal>] ; Asc 46
' [<negative>] ; Asc 45
If (((Asc(e.KeyChar) < 45) Or (Asc(e.KeyChar) > 57)) _
And (Asc(e.KeyChar) <> 8)) _
Or (Asc(e.KeyChar) = 47) Then Exit Sub

If Asc(e.KeyChar) = 45 Then
'Only allow one negative symbol in a textbox (and only in the first position)
If Len(txtCelsius.Text) <> 0 Then Exit Sub
End If

If Asc(e.KeyChar) = 46 Then
'Only allow one decimal in a textbox
If InStr(txtCelsius.Text, ".", CompareMethod.Text) <> 0 Then Exit Sub
End If
End Sub
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~