如何判断文本框里输入的是不是数字,而不能输入字符

解决方案 »

  1.   

    Private Sub Text1_KeyPress(KeyAscii As Integer)
    If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0
    End Sub
      

  2.   

    Private Sub Text1_KeyPress(KeyAscii As Integer)
    If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0
    End Sub
    思路很巧,学到了,谢谢
      

  3.   

    Private Sub Text1_KeyPress(KeyAscii As Integer)
        
        If KeyAscii > 48 And KeyAscii < 57 Then
            KeyAscii = 0
        End If
    End Sub
      

  4.   

    另外 使用 IsNumeric 也可以判断if IsNumeric (text1.text) then
        msgbox "输入的真的全部都是数字,没骗你"
    end if
      

  5.   

    引用regular expression
    '使用正则表达式也行!
    Dim myreg As New RegExp
    Private Sub Form_Load()
        myreg.IgnoreCase = True
        myreg.Pattern = "^[0-9]+$"
    End SubPrivate Sub Text1_Change()
    If myreg.Test(Text1.Text) = False Then
    MsgBox "wrong"
    End If
    End Sub
      

  6.   

    begincsharp 的要学学,也许还能判断别的!我得试试!
      

  7.   

    Private Sub Text1_KeyPress(KeyAscii As Integer)'相关字符的ASCII码'字符: (退格键) +(正号),(逗号) -(负号)'Ascii: 8 43 44 45'字符: .(小数点) /(斜杠) 0~9' Ascii: 46 47 48~57'退格键有效If KeyAscii = 8 Then Exit Sub'正号(+)、负号(-)、小数点及数字以外的字符视为无效键入If KeyAscii < Asc("+") Or KeyAscii = Asc(",") Or KeyAscii = Asc("/") Or KeyAscii > Asc("9") ThenKeyAscii = 0Exit SubEnd If'正负号(未被选取)前不能再键入字符If Left(Text1.Text, 1) = "+" Or Left(Text1.Text, 1) = "-" ThenIf Text1.SelStart = 0 ThenIf InStr(Text1.SelText, "+") = 0 And InStr(Text1.SelText, "-") = 0 ThenKeyAscii = 0Exit SubEnd IfEnd IfEnd If'键入小数点If KeyAscii = Asc(".") Then'原TextBox中已有小数点If InStr(Text1.Text, ".") > 0 Then'原TextBox中的小数点未被选取,作为无效处理(不能有两个小数点)If InStr(Text1.SelText, ".") = 0 ThenKeyAscii = 0End If'原TextBox中的小数点已被选取(选取的部分将被替换),键入有效End If'原TextBox中没有小数点,键入有效,可以是首位Exit SubEnd If'键入正号或负号If KeyAscii = Asc("+") Or KeyAscii = Asc("-") Then'原TextBox中已有正号或负号If InStr(Text1.Text, "+") > 0 Or InStr(Text1.Text, "-") > 0 Then'原TextBox中的正负号未被选取,作为无效处理(不能有两个正负号)If InStr(Text1.SelText, "+") = 0 And InStr(Text1.SelText, "-") = 0 ThenKeyAscii = 0End If'原TextBox中的正号或负号已被选取(选取的部分将被替换),键入有效Else '原TextBox中没有正号或负号'光标不位于首位,正号或负号也无效If Text1.SelStart > 0 ThenKeyAscii = 0End If'光标只有在首位时正号或负号才键入有效End IfEnd IfEnd Sub
    Private Sub Text1_Change()Static a As String '暂存文本框中的数据Static i As Integer '光标位置变量'允许清空TextBox,允许首先键入的是'“+”、“-”If Text1.Text = "" Or Text1.Text = "+" Or Text1.Text = "-" Then'保存目前数据a = Text1.TextExit SubEnd If'若文本改变后的内容为非数值类型,则键入或粘贴的数据无效If Not IsNumeric(Text1.Text) Then'保存光标原来位置i = Text1.SelStart '- Len(Text1.Text) + Len(a)'恢复原数据Text1.Text = a'恢复光标位置If i < 0 Then i = 0Text1.SelStart = iEnd If'保存目前数据a = Text1.TextEnd Sub
      

  8.   

    我这个可以输入数字小数点以及退格键,且小数点只能输入一次
    If KeyAscii <> 8 And KeyAscii <> 46 And (KeyAscii < 48 Or KeyAscii > 57) Then
            KeyAscii = 0
            MsgBox "你不能输入非数字的字符!", vbOKOnly + vbCritical, "提示框"
        ElseIf KeyAscii = 46 Then
            If f = 0 Then
                f = 1
            Else
                KeyAscii = 0
                MsgBox "你不能输入两个小数点!", vbOKOnly + vbCritical, "提示框"
            End If
        End If
      

  9.   

    以上代码在Private Sub Text2_KeyPress(KeyAscii As Integer)中写
      

  10.   

    给你一个很好用的函数,不仅仅可以判断数字,还可以自定义允许使用的字符。
    '*****************************************************************
    '** 名  称: ValiText
    '**
    '** 参  数: KeyIn          As Integer  '输入的键值
    '**         ValidateString As String   '允许使用的字符
    '**         EditAble       As Boolean  '是否处於编辑模式
    '**                                    '(是否允许使用BackSpace键)
    '**
    '** 返回值: As Integer                 '返回键值(ASCII的键值)
    '**
    '** 调  用:
    '**         Private Sub Text1_KeyPress(KeyAscii As Integer)
    '**             KeyAscii = ValiText(KeyAscii, "0123456789", True)
    '**         End Sub
    '**
    '*****************************************************************
    Function ValiText(KeyIn As Integer, ValidateString As String, EditAble As Boolean) As Integer
        Dim Validatelist As String
        Dim Keyout As Integer
        
        If EditAble = True Then
            Validatelist = UCase(ValidateString) & Chr(8) 'vbKeyBack
        Else
            Validatelist = UCase(ValidateString)
        End If
        
        If InStr(1, Validatelist, UCase(Chr(KeyIn)), 1) > 0 Then
            Keyout = KeyIn
        Else
            Keyout = 0
        End If
        ValiText = Keyout
    End Function
      

  11.   

    为什么不用MaskEDBox控件呢?很好用的呀.
      

  12.   

    Private Sub Text1_KeyPress(KeyAscii As Integer)if  not isnumeric(text1.text & char(keyascii)) then
       msgbox "不是数字
       keyascii=0
    end ifend sub 
           
    或者用MaskEDBox控件