有没有办法让Text控件只能输入数字,输入其他字符则无效

解决方案 »

  1.   

    '在窗体上放Text2、Text1Option ExplicitSub TestKey(KeyAscii)
       '测试0-9,BS,DEL,小数点,-
      If (KeyAscii < 48 Or KeyAscii > 57) And (KeyAscii <> 46) And (KeyAscii <> 8) And (KeyAscii <> 13) And KeyAscii <> 45 Then
         Beep
         KeyAscii = 0
      End If
    End Sub
    Private Sub Text1_KeyPress(KeyAscii As Integer)
    TestKey KeyAscii
    End Sub'只能输入等于等于○的正数
    Sub TestKey_Positive_Not_Point(KeyAscii)
       '测试0-9,BS,DEL,-
      If (KeyAscii < 48 Or KeyAscii > 57) And (KeyAscii <> 8) And (KeyAscii <> 13) Then
         Beep
         KeyAscii = 0
      End If
    End SubPrivate Sub Text2_KeyPress(KeyAscii As Integer)
    TestKey_Positive_Not_Point KeyAscii
    End Sub
      

  2.   

    Private Sub Text1_Validate(Cancel As Boolean)
    if Asc ("Text2.text")<48 or Asc ("Text2.text")>57 then Cancel=true
    End Sub
      

  3.   

    解决办法如下:Private Sub Text1_Change()
        If IsNumeric(Text1.Text) = False Then
            SendKeys "{BackSpace}"
        End If
    End Sub
      

  4.   

    Private Sub txtFee_KeyPress(KeyAscii As Integer)
        Dim strNumbers As String
        strNumbers = "1234567890" + Chr(8) + Chr(46)
        
        If InStr(strNumbers, Chr(KeyAscii)) = 0 Then
            KeyAscii = 0
        End If
    End Sub
      

  5.   


    Private Sub Text1_Validate(Cancel As Boolean)
    for i=1 to len(text)
    if Asc (mid (text,i,1))<48 or Asc (mid (text,i,1))>57 then Cancel=true
    next
    End Sub
      

  6.   

    Private Sub text1_KeyPress(KeyAscii As Integer)
        If KeyAscii < Asc("0") Or KeyAscii > Asc("9") Then
                KeyAscii = 0
               
        End If
    End Sub
      

  7.   

    http://expert.csdn.net/Expert/topic/2010/2010081.xml?temp=.7900049
      

  8.   

    Private Sub txtAR_KeyPress(KeyAscii As Integer)
        Select Case KeyAscii
            Case 48 To 57:
                KeyAscii = KeyAscii
            Case 46:        '小数点
                If intj = 0 Then
                    KeyAscii = KeyAscii
                    intj = 1
                Else
                    KeyAscii = 0
                End If
            Case 8:
                KeyAscii = KeyAscii
            Case 9:
                KeyAscii = KeyAscii
            Case Else
                KeyAscii = 0
        End Select
    End Sub
      

  9.   

    Public Function NumbersOnly(tBox As TextBox)
    Dim DefaultStyle As Long
    DefaultStyle = GetWindowLong(tBox.hWnd, GWL_STYLE)
    NumbersOnly = SetWindowLong(tBox.hWnd, GWL_STYLE, DefaultStyle Or ES_NUMBER)
    End FunctionPublic Function UpperCaseOnly(tBox As TextBox)Dim DefaultStyle As Long
    DefaultStyle = GetWindowLong(tBox.hWnd, GWL_STYLE)
    UpperCaseOnly = SetWindowLong(tBox.hWnd, GWL_STYLE, DefaultStyle Or ES_UPPERCASE)End Function
    Public Function LowerCaseOnly(tBox As TextBox)
    Dim DefaultStyle As Long
    DefaultStyle = GetWindowLong(tBox.hWnd, GWL_STYLE)
    LowerCaseOnly = SetWindowLong(tBox.hWnd, GWL_STYLE, DefaultStyle Or ES_LOWERCASE)
    End Function
      

  10.   

    Public Function sffunLimitNumber(ByVal IntVal As Integer) As Integer
    '-------------------1-------------------
    '目    的:只允许在文本框内输入数字、退格、删除及回车键
    '输    入:ByVal IntVal As Integer,任意的键值
    '被传递值:无
    '返 回 值:过滤后的键值
    '输    出:无
    '注    解:
    '用    法:在文本框的KeyPress事件中输入KeyAscii = sffunLimitNumber(KeyAscii)即可
    '修 订 版:
    '-------------------1-------------------
    If (IntVal <> vbKeyDelete) _
    And (IntVal <> vbKeyBack) _
    And (IntVal <> 13) _
    And (IntVal < 48 Or IntVal > 57) Then
        IntVal = 0
    End If
    sffunLimitNumber = IntValEnd Function
      

  11.   

    我也觉得只允许输入数字没有这么麻烦啊在keypress事件中写入代码,检测输入的字符的ascii码,如果不是数字或删除或退格,那么就keyascii=0不就完事了?
      

  12.   

    用 lihonggen0(李洪根,用.NET,标准答案来了) 的
      

  13.   

    用 lihonggen0(李洪根,用.NET,标准答案来了) 的
      

  14.   

    lylmsdn(创影)的方法也不错,我就是这样用的