校验控件数组的index ..也就是你说的i
if i=0 then 只能输入数字
if i=3 or i=4 or i=5 the 只能输入文字
我自己写了一个,按你说的文本框数组,0-5,其中0只能输入数字,3-5只能输入文字(字母a-z,A-Z),然后1和2可以随便输入..
全部代码:
Private Sub Form_Load()
For i = 0 To 5
Text1(i) = ""
Next
End Sub
Private Sub Text1_KeyPress(Index As Integer, KeyAscii As Integer)
KeyAscii = xy(Index, KeyAscii)
End Sub
Function xy(Index As Integer, KeyAscii As Integer) As Integer
xy = 0
If KeyAscii = 8 Or KeyAscii = 46 Then
xy = KeyAscii '如果输入是del或退格,允许
Else
If Index = 0 And KeyAscii > 47 And KeyAscii < 58 Then
xy = KeyAscii 'i=0的文本框只能输入数字0-9
ElseIf (Index = 3 Or Index = 4 Or Index = 5) And ((KeyAscii > 64 And KeyAscii < 91) Or (KeyAscii > 96 And KeyAscii < 123)) Then
xy = KeyAscii 'i=3.4.5的文本框只能输入字母a-z,A-Z
ElseIf Index = 1 Or Index = 2 Then
xy = KeyAscii 'i=1,2的文本框随便输入
End If
End If
End Function
我给你一个动态判断的模型
Private Sub Text1_Change()
If Text1.Text <> "" Then
If Asc(Right(Text1.Text, 1)) < 0 Or Asc(Right(Text1.Text, 1)) > 255 Then
Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1)
MsgBox "不能输入汉字!", vbOKOnly, "输入非法字符"
End If
End If
End Sub
在Text的keypress事件中判断ASCII码,条件是>0 <255