菜 :怎样使文本框内只能输入数字?

解决方案 »

  1.   

    Private Sub Text1_KeyPress(KeyAscii As Integer)
        If KeyAscii >= &H20 And (KeyAscii < &H30 Or KeyAscii > &H39) Then
            KeyAscii = 0
        End If
    End Sub
      

  2.   

    &H20 空格的ASC码&H30 1的ASC码
    &H39 9的ASC码KeyAscii >= &H20 '作用是不把控制字符屏蔽掉,如还可以用Backspace键删除字符。
    KeyAscii < &H30 Or KeyAscii > &H39 '作用就是限制字符要落在0~9之间。
      

  3.   

    Private Sub Textl_KeyPress(KeyAscii As Integer)
    If keyAscii>=33 Then
    If KeyAscii<=vbkey9 And KeyAscii>=vbKey0 Then
    Else 
    KeyAscii=0
    End If
    End If
    End Sub 
      

  4.   

    用函数:IsNumeric()
    例如:
       if not isnumeric(text1.text)  then
          msgbox"非法字符,请输入数字"
          text1.text=" " 
          text1.setfocus
       end if
      

  5.   

    Private Sub Text1_LostFocus()
    Dim i As Integer '定义一个整形i
    On Error GoTo ERROR 
    '设置错误陷阱,如果Text1.Text不是整数跳到ERROR
    i = Text1.Text
    Exit Sub 
    ERROR:
    MsgBox "请输入一个整数",64
    Text1.SetFocus 
    End Sub