VB里的TEXT控件只能输入数字的代码

Const xStr As String = "0123456789"
'先声明一个常量,并把你想禁用或允许输入的内容赋值给它

Private Sub Text1_KeyPress(KeyAscii As Integer)
'只能输入数字
KeyAscii = IIf(InStr(xStr & Chr(8), Chr(KeyAscii)), KeyAscii, 0)
End Sub

Private Sub Text2_KeyPress(KeyAscii As Integer)
'只能输入数字以外的字符
KeyAscii = IIf(InStr(xStr, Chr(KeyAscii)), 0, KeyAscii)
End Sub

 

posted @ 2016-09-22 17:37  comsky  阅读(2734)  评论(0编辑  收藏  举报