Private Sub Text1_KeyPress(KeyAscii As Integer)
If (Chr(KeyAscii) > "9" Or Chr(KeyAscii) < "0") And Chr(KeyAscii) <> "." Then
    KeyAscii = 0
End If
End Sub或者
Private Sub Text1_KeyPress(KeyAscii As Integer)
  Select Case KeyAscii
      Case Asc("-") '允许负数
            If Text1.SelStart = 0 Then
              If Left(Text1.Text, 1) = "-" Then
                  KeyAscii = 0
                  Beep
              End If
            Else
              KeyAscii = 0
              Beep
            End If
        Case 8
              '无变化,退格键不屏蔽
        Case Asc(" ") '32
            If Text1.SelLength = 0 Then
                KeyAscii = 0
            End If
        Case Asc(".") '46 '允许小数点
            If InStr(Text1.Text, ".") Then
                KeyAscii = 0
            End If
        Case Is < Asc(0) '48
              KeyAscii = 0
        Case Is > Asc(9) '57
              KeyAscii = 0
  End Select
End Sub  

解决方案 »

  1.   

    Private Sub txtZJS_KeyPress(KeyAscii As Integer)
        If KeyAscii = 13 Then comSYBM.SetFocus
        If Not (KeyAscii > 47 And KeyAscii < 58 Or KeyAscii = 46) Then KeyAscii = 0
    End Sub
      

  2.   

    个人感觉不应在KeyPress事件中判断,如果我点击鼠标右键粘贴的话,一样可以在文本框中输入数字。
      

  3.   

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

  4.   

    很简单:
    Private Sub Text1_Change()
        Text1.Text = Val(Text1.Text)
    End Sub
      

  5.   

    应该在keypress中判断,同时为了防止粘贴时将非数字的字符进入text中,应同时在mouse_down中判断是否粘贴,粘贴时将剪切版中的非数字字符删除。
        text1.text = mFilterChar(clipboard.gettext())
    mFilterChar是你必须编的函数。