本人要编一个文本框的功能如下:1。只能输入数字键 2。输入的数字在0到100之间,超出报错。
另外,怎么才能限制在文本框里输入的字符,只能是中文汉字和英文字母?
请各位高手赐教。THANKS!!

解决方案 »

  1.   

    可以根据键位的ascii码值来判断,例如KeyPress事件,要判断在0和100之间可以将文本框里的字符转换成数值判断大小。
      

  2.   

    先回答你第一个问题,我写一段示例代码给你吧,假设这个文本框是text1
    Private Sub text1_Change()
        Dim intChar As Integer
        '取text1的最右边(即最新输入的一个字符),转换成ASCII码存放到intChar里    
        intChar=Asc(Right(text1.text,1))
        If intChar<48 Or intchar>57 Then '0和9的ASCII值分别为48和57
           '提示输入的不是数字键,错误处理你就自己写吧
        Else If Val(text1.text)>=0 Or Val(text1.text)<=100 Then 
           '提示输入的数字在0至100之间,错误处理你也自己定吧。
        End If
    End Sub再回答你第二个问题,其实也是一样的,在text1_Change()里写一段代码取最右边的第一个字符,返回其ASCII值,不在48至57之间就不是数字了,不过回为有汉字,所以ASC函数要换一下,换成ASCW么样?Any question?
      

  3.   

    Dim t As String
    Private Sub Text1_KeyPress(KeyAscii As Integer)
     If KeyAscii < 48 Or KeyAscii > 57 Then
        KeyAscii = 0
       Else
        t = t & Chr(KeyAscii)
     End If
     If Int(t) > 100 Then MsgBox "error": KeyAscii = 0
    End Sub
      

  4.   

    Dim t As StringPrivate Sub Command1_Click()
     p = 2#
    End SubPrivate Sub Text1_KeyPress(KeyAscii As Integer)
     If KeyAscii < 48 Or KeyAscii > 57 Then
        KeyAscii = 0
       Else
        t = t & Chr(KeyAscii)
     End If
     If Int(t) > 100 Then MsgBox "error": KeyAscii = 0
    End SubPrivate Sub Text2_KeyPress(KeyAscii As Integer)
     '中文输入如果我没记错,中文的keyascii<0
     If KeyAscii >0 Then KeyAscii = 0
    End SubPrivate Sub Text3_KeyPress(KeyAscii As Integer)
    '输入英文
      If Asc(UCase(Chr(KeyAscii))) < 65 Or Asc(UCase(Chr(KeyAscii))) > 90 Then KeyAscii = 0
    End SuB
      

  5.   

    Dim Mytext As StringFunction TestText(KeyIn As Integer, ListString As String, EditBasp As Boolean) As Integer
      Dim TestDATList As String '定义限制字符表变量
      Dim KeyOut As Integer '返回值变量
      If EditBasp = True Then '测试BACKSPACE键是否有效
        TestDATList = UCase(ListString) & Chr(8) '得到含BACKSPACE键字符的大写表
      Else
        TestDATList = UCase(ListString) '得到无BACKSPACE键字符的大写表
      End If
      If InStr(1, TestDATList, UCase(Chr(KeyIn)), 1) > 0 Then '键值是否在表中
        KeyOut = KeyIn '是则附键值
      Else
        KeyOut = 0 '否则键值无效
      End If
      TestText = KeyOut '返回结果
    End FunctionPrivate Sub Form_Load()
    Dim i As Integer
      Mytext = "0123456789"
      Mytext = Mytext + "你允许输入的其他字符!!"
    End SubPrivate Sub Text1_KeyPress(KeyAscii As Integer)
      KeyAscii = TestText(KeyAscii, Mytext, True) 'Text1只接受mytext规定的字符。
    End Sub
    我在  http://expert.csdn.net/Expert/topic/1768/1768883.xml?temp=.7372095还发了一个和这个程序相似的(其实是一样的)程序,用来禁止指定字符的输入,有兴趣可以去看看~~我的程序可是在VB里通过测试后才拿出来的。绝对好用!^_^像作广告~~
      

  6.   

    select case keyascii
        case 13,27,8'enter,esc,del
        case else
            select  case chr(keyascii)
                  case 0 to 100
                  case else
                      keyascii=0
                      msgbox "提示"
            end select
    end select
      

  7.   

    应该很好实现的~
    用ascii码来做
      

  8.   

    ASCII太麻烦,像我那个程序是一劳永逸~哈哈!天才的我太天才啦!哈哈!!~~~
      

  9.   

    第二个问题好说啊,在第一个问题解决的基础上,只要限制文本框的最大长度是2就可以了
    好象是maxlen=2吧,在属性栏中就有