vb textbox 禁止输入重复数字

来源:百度知道 编辑:UC知道 时间:2024/06/24 01:46:06
我现在用这段代码写成只能输入数字,现在想加一个条件就是输入的不能有重复的数字如输入了则弹出“不能有重复的” ,然后删除这个最后输入的数,该怎么写,如我输入04这时我继续输入0或4,弹出“不能有重复的”,然后删除最后输入的0或4,这时text里还只是04
If Not IsNumeric(Text4(1).Text) And Len(Text4(1).Text) <> 0 Then
Text4(1).Text = Mid(Text4(1).Text, 1, Len(Text4(1).Text) - 1)
MsgBox "必须输入数字!", vbOKOnly, "输入数据类型错误"
Text4(1).SelStart = Len(Text4(1).Text)
ElseIf Len(Text4(1).Text) = 0 Then
Text4(1).Text = ""
Text4(1).SelStart = Len(Text4(1).Text)
End If
怎么改

直接在KeyPress里加代码更好:
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 8 Then Exit Sub
If KeyAscii < 48 Or KeyAscii > 57 Then
MsgBox "必须输入数字!", vbOKOnly, "输入数据类型错误"
KeyAscii = 0
ElseIf InStr(Text1.Text, Chr(KeyAscii)) > 0 Then
MsgBox "不能有重复的", vbOKOnly, "输入数据类型错误"
KeyAscii = 0
End If
End Sub

Dim a(10) As Integer: Dim b As Integer
Private Sub Text1_Change()
For i = 1 To 10
If Val(Right(Text1, 1)) = a(i) Then
MsgBox "有重复啦!因为要去吃饭,待会回来再帮你完善!"
Else
End If
Next
a(b + 1) = Val(Right(Text1, 1))
b = b + 1

End Sub

Gerald_Bond 你做的不错 呵呵

Private Sub Text1_Change()
If Not IsNumeric(Text1.Text) And Len(Text1.Text) <> 0 Then
Text1.Text = Mid(Text1.Text, 1, Len(Text1.Text) - 1)
MsgBox "必须输入数字!", v