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
Function 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 Function
Private Sub Form_Load()
Dim i As Integer
Mytext = "0123456789"
Mytext = Mytext + "你允许输入的其他字符!!"
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
KeyAscii = TestText(KeyAscii, Mytext, True) 'Text1只接受mytext规定的字符。
End Sub
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
Private Sub Text2_KeyPress(KeyAscii As Integer)
'中文输入如果我没记错,中文的keyascii<0
If KeyAscii >0 Then KeyAscii = 0
End Sub
Private Sub Text3_KeyPress(KeyAscii As Integer)
'输入英文
If Asc(UCase(Chr(KeyAscii))) < 65 Or Asc(UCase(Chr(KeyAscii))) > 90 Then KeyAscii = 0
End SuB
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
先回答你第一个问题,我写一段示例代码给你吧,假设这个文本框是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