如何让text控件中只输入1、2、3、4、5、6、7、8、9、0这几个数字,其他的内容都不允许输入?

解决方案 »

  1.   

    Private Sub Text1_KeyPress(KeyAscii As Integer)
    If IsNumeric(Chr(KeyAscii)) = False Then KeyAscii = 0
    End Sub
      

  2.   

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

  3.   


    'text框的 keypress事代码
    '在有需要的地方这样调用 presskey 就可以了
    Private Sub text1_KeyPress(KeyAscii As Integer)
        PressKey KeyAscii
    End Sub'按键处理
    '如果需要输入小数点正负号,请改变常量 allowkey
    private Sub PressKey(ByRef keyVal As Integer)
          const allowKey As String="0123456789"
          If InStr(allowKey, Chr(keyVal)) = 0 And keyVal <> 8 Then keyVal = 0
    End Sub
      

  4.   

    '根据KeyAscii范围
    Private Sub Text1_GotFocus()
        SelTextBox Text1
    End SubPrivate Sub Text1_KeyPress(KeyAscii As Integer)
        If KeyAscii > 58 Or KeyAscii < 47 Then
            MsgBox "Error", vbOKOnly, vbExclamation
            Call Text1_GotFocus
        End If
    End SubSub SelTextBox(tb As TextBox)
        tb.SelStart = 0                     ' Set selection start.
        tb.SelLength = Len(tb.Text)         ' Set selection length.
    End Sub