在textbox的KEYPRESS事件中检测ASCII码。

解决方案 »

  1.   

    假定你的文本框叫做text1,示例如下:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
       If KeyAscii < Asc("0") Or KeyAscii > Asc("9") Then
          KeyAscii = 0        '取消字符。
          msgbox"请输入数字"   '出错提示
       End If
    End Sub
      

  2.   

    可是上边的代码运行之后如果我输入91的而我只想输入9这时候用退格键就不行还得把光标移动1的前面用DEL删除好麻烦。
      

  3.   

    Private Sub Text1_Change()If Not IsNumeric(Text1.Text) Then
    If Text1.Text = "" Then Exit Sub
    Text1.Text = Mid(Text1.Text, 1, Len(Text1.Text) - 1)
    'MsgBox "只允许输入数字!"
    Text1.SelStart = Len(Text1.Text)
    End If
    End Sub 
      

  4.   

       在使用Visual Basic开发应用程序时,TextBox控件是最常用的(特别
    对于数据库程序),以下是笔者在开发应用程序时的一些技巧,希望对爱好
    VB的朋友有帮助。1、 限制TextBox只能输入数字。    我们只需要判断KeyAscii是否在48-57之间就可以达到目的,但我们还会
    要用到退格键的,所以必须加上KeyAscii <> 8。Private Sub Text1_KeyPress(KeyAscii As Integer)
        If KeyAscii <> 8 And KeyAscii < 48 Or KeyAscii > 57 Then
            Beep
    KeyAscii = 0
        End If
    End Sub2、自动将输入的英文转为小写/大写。     如果在KeyPress事件里使用Ucase$和Lcase$,那么当你输入abc时,Text
    Box里的结果会是CBA(具体原因这里不多说),我们只好判断它的KeyAscii,
    刚好大写与小写之间是相差32,所以……看下面的程序。'大写转小写
    Private Sub Text1_KeyPress(KeyAscii As Integer)
        If KeyAscii >= 65 And KeyAscii <= 90 Then
            KeyAscii = KeyAscii + 32
        End If
    End Sub'小写转大写
    Private Sub Text2_KeyPress(KeyAscii As Integer)
        If KeyAscii >= 97 And KeyAscii <= 122 Then
    KeyAscii = KeyAscii - 32
        End If
    End Sub3、使两个TextBox同步的方法。    这种方法怎么解释呢?第二个等于第一个就行了:-)。Private Sub Text1_KeyPress(KeyAscii As Integer)
        Text2=Text1
    End Sub4、按下 Enter 键之后,光标自动移到下一个控件。    当KeyAscii为13(回车)时,Send一个Tab键给它就行了。Private Sub Text1_KeyPress (KeyAscii As Integer)
        If KeyAscii = 13 Then
            SendKeys “{tab}”
            KeyAscii = 0
        End If
    End Sub5、当光标移到TextBox时自动选定其中的整串文字。    当对象得到焦点时,从最前面开始选取(SelStart=0),选取的
    长度为文本长度(Len(Text1))。Private Sub Text1_GotFocus()
        Text1.SelStart = 0
        Text1.SelLength = Len(Text1)
    End Sub6、当光标移到TextBox时改变其文字颜色。    当TextBox得到焦点时(GotFocus)改变文本颜色,失去焦点时(
    LostFocus)将还原文本颜色,ForeColor是设置文本的前景色。'当光标移到TextBox时文字设置为红色
    Private Sub Text1_GotFocus()
        Text1.ForeColor = vbRed
    End Sub'当光标移出TextBox时文字设置为黑色
    Private Sub Text1_LostFocus()
        Text1.ForeColor = vbBlack
    End Sub    以上代码只要将Text1.ForeColor改为Text1.BackColor即可改变TextBox
    的背景色。我补充一些:
    在使用textbox的时候,我喜欢把它的lock属性设为true,所有textbox里显示的内容都是在程序里控制设定的,这样的好处是利于控制,而且应用到password上,只要判断输入字符的多少,让文本框按数量显示星号,真正的输入仍然存在变量里,就比用textbox.passwordchar要好
      

  5.   

    恕我直言,以上几种方法都不好。漏洞太大。比如,ctrl+v 就没办法了。这样就可以了:Dim lStyle As Long
    lStyle = GetWindowLong(txthWnd, -16)
    lStyle = lStyle Or &H2000
    SetWindowLong txthWnd, -16, lStyle这样就可以使 txthWnd 对应的那个文本框只接受数字了。至于里面的 -16 和 &h2000 等
    常量,是因为我以前在写程序的时候偷懒,省掉了定义常量这一步,直接把常量的数值写近来了……
      

  6.   

    Private Sub Text1_KeyPress(KeyAscii As Integer)
      If KeyAscii < Asc("0") Or KeyAscii > Asc("9") Then
          KeyAscii = 0        '取消字符。
          msgbox"请输入数字"  '出错提示
      End If
    End Sub 
    或者在用户修改完了以后用isnumric(text1.text)
      

  7.   

    楼上的 :用" 111111"试试你的代码(请拷贝过去),并且数据存入SQLSERVER数据库试试看看结果是什么,哈哈
      

  8.   

    Private Sub text1_KeyPress(KeyAscii As Integer)
      
     KeyAscii = ConfineInput(KeyAscii, "0123456789") '只能输入数字
    ‘KeyAscii = ConfineInput(KeyAscii, "asdfgghj56789") '只能输入asdfgghj56789
     
    end sub'限制用户的输入
    Public Function ConfineInput(para_KeyAscii As Integer, para_ConfineStr As String) Dim sConfineStr As String
     
     sConfineStr = UCase(para_ConfineStr) + Chr(13) + Chr(8) + Chr(27)
     If InStr(1, sConfineStr, UCase(Chr(para_KeyAscii)), vbTextCompare) = 0 Then
        ConfineInput = 0
     Else
       ConfineInput = para_KeyAscii
     End If
     End Function
    很好用的,试试!记住加分,加多少无问题。(:
      

  9.   

    -16 应该是 GWL_STYLE
    &H2000 应该是 ES_NUMBER
      

  10.   

    再声明一次:在 KeyPress、KeyDown 等事件里面判断都是不可靠的,API 已经有很好的解决办法。这是完整的代码:Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA"
    (ByVal hwnd As Long, ByVal nIndex As Long) As Long
    Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA"
    (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    Const GWL_STYLE = (-16)
    Const ES_NUMBER = &H2000&Public Sub SetNumber(NumberText As TextBox, Optional Flag As Boolean = True)
        Dim curstyle As Long, newstyle As Long    'retrieve the window style
        curstyle = GetWindowLong(NumberText.hwnd, GWL_STYLE)    If Flag Then
           curstyle = curstyle Or ES_NUMBER
        Else
           curstyle = curstyle And (Not ES_NUMBER)
        End If    'Set the new style
        newstyle = SetWindowLong(NumberText.hwnd, GWL_STYLE, curstyle)
        'refresh
        NumberText.Refresh
    End Sub