Private Sub TEXT1_KeyPress(KeyAscii As Integer)
If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 8 Then
Beep
KeyAscii = 0
End If
x = Chr(KeyAscii)
For i = 1 To Len(Text1.Text)
If Mid(Text1.Text, i, 1) = x Then Beep: KeyAscii = 0
Next i
End Sub
'完全符合你的要求,但是我提醒一下,text不仅可以输入文字,也可以用鼠标右键粘贴进去
Private Sub Text1_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case Asc("-") '允许负数
If Text1.SelStart = 0 Then
If Left(Text1.Text, 1) = "-" Then
KeyAscii = 0
Beep
End If
Else
KeyAscii = 0
Beep
End If
Case 8
'无变化,退格键不屏蔽
Case Asc(" ") '32
If Text1.SelLength = 0 Then
KeyAscii = 0
Beep
End If
Case Asc(".") '46 '允许小数点
If InStr(Text1.Text, ".") Then
KeyAscii = 0
Beep
End If
Case Is <= Asc(9)
If InStr(Text1.Text, Chr(KeyAscii)) <> 0 Then
KeyAscii = 0
Beep
End If
Case Is < Asc(0) '48
KeyAscii = 0
Beep
Case Is > Asc(9) '57
KeyAscii = 0
Beep