检查文本框输入是否为数字 [英] Checking to see if text box input is numeric

查看:89
本文介绍了检查文本框输入是否为数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对此进行了一些研究,但仍然无法让我的程序运行.我只需要检查文本框以查看用户输入是否为数值(."和或/"除外)

I've done some research on this and still cannot get my program to work. I simply need to check the text box to see if the user input is a numeric value, or not (with the exception of a "." and or "/")

到目前为止我的代码,

 Private Sub Num1_KeyPress(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Num1.KeyPress
    Dim UserEntry As Boolean
    If UserEntry = IsNumeric(False) Then
        MessageBox.Show("That's not numeric!")
    End If
End Sub

推荐答案

我认为你最好使用 TextBox.KeyUp 事件,它通过 KeyEventArgs.试试这个:

I think you better be using TextBox.KeyUp event, it passes the KeyEventArgs. Try this :

Private Sub Num1_KeyUp(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles Num1.KeyUp

    Dim isDigit As Boolean = Char.IsDigit(ChrW(e.KeyValue))
    Dim isKeypadNum As Boolean = e.KeyCode >= Keys.NumPad0 And e.KeyCode <= Keys.NumPad9
    Dim isBackOrSlashOrPeriod As Boolean = (e.KeyCode = Keys.Decimal Or e.KeyCode = Keys.Oem2 Or e.KeyCode = Keys.Back Or e.KeyCode = Keys.OemPeriod)

    If Not (isDigit Or isKeypadNum Or isBackOrSlashOrPeriod) Then
        MessageBox.Show("That's not numeric!")
    End If

End Sub

这篇关于检查文本框输入是否为数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆