Char.IsNumber(ChrW(e.KeyCode)

时间:2016-08-13 13:30:36

标签: .net vb.net

I am trying to prevent user from entering alphabets into TextBox1. I want user to only enter numbers. I have written following code in KeyUp event of the TextBox but the program does not run as intended. The compiler evaluates the MessageBox.show no matter what data is entered. If user enters a number, the MessageBox displays message and if an alphabet is entered, even then it gets executed. Could you please tell what is wrong with the following code?

Private Sub TextBox1_KeyUp(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyUp
    If (Not Char.IsNumber(ChrW(e.KeyCode))) Then
        MessageBox.Show("Enter only numbers")
    End If
End Sub

0 个答案:

没有答案