keypress event - asc value and key. mismatch

Drstein99

Junior Contributor
Joined
Sep 26, 2003
Messages
283
Location
Audubon, Nj
PLEASE POST REPLIES IN VB.NET SYNTAX ONLY

I'm using keypress event, to trigger validation:

Public Sub esKeyPressInteger(ByVal sender As Object, ByVal e As KeyPressEventArgs)


and the VALUE of keys.delete (46) IS the same ASCII value of the ascii code for "." (decimal point)

How do i figure out that the char entered is only numeric, and also allow a delete or arrow key? I'm using this and it's malfunctioning:

If (Char.IsNumber(e.KeyChar) Or Asc(e.KeyChar) = Keys.Delete Or Asc(e.KeyChar) = Keys.Back) AND ASC(E.KeyChar) <> KEYS.Decimal Then
e.Handled = False
Else
e.Handled = True
End If
 
Try this:

Code:
    Dim m_blnKeyHandled As Boolean
    Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
        m_blnKeyHandled = e.KeyCode = Keys.Decimal OrElse _
                          e.KeyCode = Keys.Back OrElse _
                          e.KeyCode = Keys.Delete OrElse _
                          Char.IsLetter(Convert.ToChar(e.KeyValue))
    End Sub

    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        e.Handled = m_blnKeyHandled
    End Sub

Btw, I hope you're aware that a NumericUpDown control exists, specifically designed to accept numeric input.
 
hey a quick question, whta's OrElse do?,

tahnks

in response to the first post,

If (e.keycode = keys.Back Or e.keycode = keys.Delete Then
'do the same for the numbers(ex: keys.1 though 9 i beleive) or maybe "Or Char.IsNumber(e.keycode)" would work

'here you'd type in code that executes when you push thoe keys




end if
 
Back
Top