Private Sub Text2_KeyPress(KeyAscii As Integer)
'MsgBox KeyAscii
If KeyAscii <> 8 Then '可以用退格键改数字
If KeyAscii > Asc("9") Or KeyAscii < Asc("0") Then
KeyAscii = 0
End If
End If
End Sub
以上限制了就不能改了,我的还可以改的
标准答案来了:
先把text文本框的maxlength属性设置2,再"
Private Sub Text1_KeyPress(KeyAscii As Integer)
If Not IsNumeric(Chr(KeyAscii)) Then
Text1.Locked = True
Else
Text1.Locked = False
End If
End Sub
Private Sub Text2_KeyDown(Index As Integer, KeyCode As Integer, Shift As Integer)
If KeyCode < 48 Or KeyCode > 57 Then
MsgBox "Can't enter letter but number"
Exit Sub
End If
End Sub
类似的问题前几天才讨论过.没有其它的限制了吗?
Private Sub Form_Load()
Text1.MaxLength = 2
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii < Asc("0") Or KeyAscii > Asc("9") Then KeyAscii = 0
End Sub