如何让该文本框只接收文字,其余输入报错。我知道用if语句判断,我想知道具体的代码。

解决方案 »

  1.   

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

  2.   

    Private Sub Text1_KeyPress(KeyAscii As Integer)
    '允许汉字、字符、数字及汉字中的字符
        If (KeyAscii >= 33 And KeyAscii <= 47) Or _
        (KeyAscii >= 58 And KeyAscii <= 65) Or _
        (KeyAscii >= 91 And KeyAscii <= 96) Or _
        (KeyAscii >= 123 And KeyAscii <= 126) Then MsgBox "aa"
        Me.Caption = KeyAscii
    End Sub
      

  3.   

    Private Sub Text1_KeyPress(KeyAscii As Integer)
        If (KeyAscii >= 48 And KeyAscii <= 57) And KeyAscii <> 88 And KeyAscii <> 8 Then
            KeyAscii = 0
        End If
    End Sub
    输入数字的话不显示。
    8是"退格键"  88是"删除键"
      

  4.   

    private sub key_press(keyascii)
    end sub
      

  5.   

    不好意思上边那段没写完就发出去了补上
    private sub key_press(keyascii as integer)
        if keyascii<>8 then
             If not (KeyAscii >= 33 And KeyAscii <= 47) Or  (KeyAscii >= 58 And KeyAscii <= 65) Or KeyAscii >= 91 And KeyAscii <= 96) Or   (KeyAscii >= 123 And KeyAscii <= 126) then      keyascii=0
         end if
       end if
    end sub
      

  6.   

    Private Sub Text1_KeyPress(KeyAscii As Integer)
        If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 8 Then
          KeyAscii = 0
          msgbox "error!"
        endif
    End Sub