在keydown事件中写下以下代码:
if keycode<48 or keycode>57 then
   msgbox "只能输入数字"
   exit sub
end sub
这样可以判断是不是数字,但问题是非法字符还是输进去了
以前用delphi的时候这样的代码就可以控制了的
vb应该怎样控制啊?

解决方案 »

  1.   

    Private Sub Text1_KeyPress(KeyAscii As Integer)
    If KeyAscii < 48 Or KeyAscii > 57 Then
    KeyAscii = 0
    End If
    End Sub
    这样就可以只能输入数字了!
      

  2.   

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

    Private Sub Text1_KeyPress(KeyAscii As Integer)
    If KeyAscii > 44 And KeyAscii < 58 And KeyAscii <> 47 Then
    KeyAscii = 0
    End If
    End Sub你可以写个代码看你要输入的范围按键的 KeyAscii 数
    Private Sub Text1_KeyPress(KeyAscii As Integer)
      Text1 = KeyAscii
    End Sub
      

  4.   

    Private Sub Text1_KeyPress(KeyAscii As Integer)
        If Len(Text1.Text) = 0 And KeyAscii = 46 Then KeyAscii = 0
        If InStr(1, Text1.Text, ".") <> 0 And KeyAscii = 46 Then KeyAscii = 0
        If KeyAscii = 8 Or KeyAscii = 46 Then Exit Sub
        If Not IsNumeric(Chr(KeyAscii)) Then KeyAscii = 0
    End Sub
    -----------------------------
    可以有小数点,BACKSPACE ,DEL,判断一个不能为小数点,屏蔽多次输入小数点