txt_KeyPressIf (KeyAscii < 48 Or KeyAscii > 57) And (KeyAscii <> 8 And KeyAscii <> 46) Then KeyAscii = 0

解决方案 »

  1.   

    Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode < 48 Or KeyCode > 57 Then
       Exit Sub
    End If
    End Sub
      

  2.   

    '*****************************************************************
    '文本框只能输入数字Public Sub InputNumeric(KeyAscii As Integer, txtItem As TextBox)
        Select Case KeyAscii
            Case Asc("-") '允许负数
                If txtItem(i).SelStart = 0 Then
                  If Left(txtItem(i).Text, 1) = "-" Then
                      KeyAscii = 0
                      Beep
                  End If
                Else
                  KeyAscii = 0
                  Beep
                End If
            Case 8
                  '无变化,退格键不屏蔽
            Case Asc(" ") '32
                If txtItem(i).SelLength = 0 Then
                    KeyAscii = 0
                End If
            Case Asc(".") '46 '允许小数点
                If InStr(txtItem(i).Text, ".") Then
                    KeyAscii = 0
                End If
            Case Is < Asc(0) '48
                  KeyAscii = 0
            Case Is > Asc(9) '57
                  KeyAscii = 0
        End Select
    End Sub
    '*****************************************************************
      

  3.   

    用的时候只要指明
    InputNumeric keyascii,yourtextname
      

  4.   

    If Not IsNumeric(Text1(5).Text) Then
       MsgBox ("必须为数字哦")
       Exit Sub
    End If