Option ExplicitPrivate Sub Text1_KeyPress(KeyAscii As Integer) Dim s As String s = UCase("0123456789") & Chr(8) If InStr(s, UCase(Chr(KeyAscii))) <= 0 Then KeyAscii = 0 End Sub
'应该是这样 Option ExplicitPrivate Sub Text1_KeyPress(KeyAscii As Integer) Dim s As String s = UCase("0123456789") & Chr(8) & Chr(9) & Chr(10) & Chr(13) If InStr(s, UCase(Chr(KeyAscii))) <= 0 Then KeyAscii = 0 End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)If KeyAscii > 48 And KeyAscii < 57 Then Exit Sub If KeyAscii = 8 Then Exit SubFor a = 48 To 57 '此循环将全角数字转为半角数字 If KeyAscii = a - 23680 Then KeyAscii = a Exit Sub End If Next aKeyAscii = 0 End Sub
你还想要什么键有效,写在 keyascii=0 前面 类似于 If KeyAscii = 8 Then Exit Sub
Dim s As String
s = UCase("0123456789") & Chr(8)
If InStr(s, UCase(Chr(KeyAscii))) <= 0 Then KeyAscii = 0
End Sub
Option ExplicitPrivate Sub Text1_KeyPress(KeyAscii As Integer)
Dim s As String
s = UCase("0123456789") & Chr(8) & Chr(9) & Chr(10) & Chr(13)
If InStr(s, UCase(Chr(KeyAscii))) <= 0 Then KeyAscii = 0
End Sub
If KeyAscii = 8 Then Exit SubFor a = 48 To 57 '此循环将全角数字转为半角数字
If KeyAscii = a - 23680 Then
KeyAscii = a
Exit Sub
End If
Next aKeyAscii = 0
End Sub
类似于 If KeyAscii = 8 Then Exit Sub
工程-》部件-》选择“microsoft masked edit control 6.0(sp3)”.
它与文本框(TEXT BOX)功能基本相似,但多了一个MASK属性,MASK属性常用的设定如下:
#—限定仅能输入数字0~9;
A— 限定输入为英文字符及数字;
?—限定仅能输入英文字符;
.—限定小数点位置;
:—限定时间分隔号;
/—限定日期分隔号
不需编写代码,只要在设计时将格式化文本框对象(MASKED EDIT)的MASK属性设计好所需格式即可。比如 #######.##可输入12.3或123456.78但不能输入字母或其他字符但你必须考虑你输入的数字的位数,例如,如果把mask属性设为###.##,那么就只可以输入123.45,而不能输入1234.56,所以,最好把位数设得足够多。这样,你不需要写一行程序,只需要设定masked edit控件的属性就可以达到你的目的了