keypress event - asc value and key. mismatch

Drstein99

Well-known member
Joined
Sep 26, 2003
Messages
283
Location
Audubon, Nj
PLEASE POST REPLIES IN VB.NET SYNTAX ONLY

Im 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? Im using this and its 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 youre aware that a NumericUpDown control exists, specifically designed to accept numeric input.
 
hey a quick question, whtas 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 youd type in code that executes when you push thoe keys




end if
 
Or else only evaluates both operands if the first is false - otherwise it knows if the first is true it doesnt matter what the second operand equates to.
 
Back
Top