Private Sub TEXTBOX_KeyPress(Index As Integer, KeyAscii As Integer)
  If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 8 Then
     KeyAscii = 0
     Beep
     Exit Sub
  End If
End Sub

解决方案 »

  1.   

    Private Sub TEXTBOX_KeyPress(Index As Integer, KeyAscii As Integer)
      If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 8 Then
         testbox.text=""
         testbox.setfocus
         Exit Sub
      End If
    End Sub
      

  2.   

    或者这样:
    Private Sub txtAge_KeyPress(KeyAscii As Integer)
        If Not ((Chr(KeyAscii) Like "[0-9]") Or KeyAscii = 8) Then
            KeyAscii = 0
        End If
    End Sub
      

  3.   

    可以用MaskEdit控件,不过麻烦!
    给你一个非常简单的代码!
    ////////只可以输入数字,如果想输入退格键的话在if语句加上去就OK了!/////
    Private Sub Text1_KeyPress(KeyAscii As Integer)
      If KeyAscii > vbKey0 Or KeyAscii < vbKey9 Then
      Else: KeyAscii = 0
      End If
    End Sub
    ////////////////////////////////////////////////////////////
      

  4.   

    Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
    Private Const GWL_STYLE = (-16)
    Private Const ES_NUMBER = &H2000Private Sub Form_Load()
      SetWindowLong TEXTBOX.hwnd, GWL_STYLE, GetWindowLong(TEXTBOX.hwnd, GWL_STYLE) Or ES_NUMBER
    End Sub
      

  5.   

    我好像前不久回答了一个这样的问题,主要是进行输入键的判断,让输入的键值在0到9的ascii之间。