VB高手,快来帮帮我

来源:百度知道 编辑:UC知道 时间:2024/05/18 15:49:39
怎么使文本框最多接受三个字符且不接受非数字键啊?
谢谢啦!!!!!!!!

我专门为你这个回答写的程序,已经测试~ 非常成功!!

Private Sub Text1_Change()
'只接受三个字符
If Len(Text1.Text) > 3 Then
Text1.Text = Left(Text1.Text, 3)
End If

'不接受非数字键(只接受数字)
For i = 1 To Len(Text1.Text)
If IsNumeric(Mid(Text1.Text, i, 1)) = False Then
'将输入的非数字替换成空字符
Text1.Text = Replace(Text1.Text, Mid(Text1.Text, i, 1), "")
End If
Next i
End Sub

Private Sub Form_Load()
Text1.MaxLength = 3
End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)
If Not (KeyAscii > Asc("0") And KeyAscii < Asc("9")) And KeyAscii <> 8 Then KeyAscii = 0 '除0~9和退格键外,其余全部禁止输入
End Sub
-------------------------------------------------------------
注:此段代码有一个漏洞,当在文本框上点击右键选择粘贴的时候,由于没有触发KeyPress事件,所以不能判断粘贴内容的合法性。你可以发挥你的聪明才智解决这个问题。

Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
If Len(Text1.Text) > 2 Or KeyCode <