Option ExplicitPrivate Sub Text1_KeyPress(KeyAscii As Integer) If KeyAscii < Asc(0) Or KeyAscii > Asc(9) Then MsgBox "ERR" KeyAscii = 0 End If End Sub
Private Sub Text1_LostFocus() If IsNumeric(Text1.Text) = False Or Val(Text1.Text) > 10 Or Val(Text1.Text) < 0 Then MsgBox "数据非法" Text1.SetFocus Text1.SelStart = 0 Text1.SelLength = Len(Text1.Text) Exit Sub End If End Sub
Private Sub txtTimeout_KeyPress(KeyAscii As Integer) If Not ((KeyAscii >= 48 And KeyAscii <= 57) Or KeyAscii = 8) Then KeyAscii = 0 Msgbox "错误,只能输入0~9的整数。",vbokonly,"发生错误" End If End Sub
If KeyAscii < Asc(0) Or KeyAscii > Asc(9) Then
MsgBox "ERR"
KeyAscii = 0
End If
End Sub
If IsNumeric(Text1.Text) = False Or Val(Text1.Text) > 10 Or Val(Text1.Text) < 0 Then
MsgBox "数据非法"
Text1.SetFocus
Text1.SelStart = 0
Text1.SelLength = Len(Text1.Text)
Exit Sub
End If
End Sub
If Not ((KeyAscii >= 48 And KeyAscii <= 57) Or KeyAscii = 8) Then
KeyAscii = 0
Msgbox "错误,只能输入0~9的整数。",vbokonly,"发生错误"
End If
End Sub
上面的几位的代码都可以,不过,条件里面应该把
vbKeyBack 0x8 BACKSPACE 键
vbKeyDelete 0x2E DELETE 键
和 小数点 考虑进去,方便输入
另外,放在KeyPress(KeyAscii As Integer)事件里面检查,仍然可以用鼠标右键粘贴非数字进去
如果你和我一样不喜欢用api的话
就在Validate(Cancel As Boolean)事件里面加相关代码来检查
呵呵
我没用过~~请说说有什么用???
object 一个对象表达式,其值为“应用于”列表中的一个对象。
KeepFocus 确定控件是否失去焦点的值。KeepFocus设置为True时,控件保持焦点。
说明Validate 事件和CausesValidation 属性协同工怍,防止控件失去焦点直到满足确定的准则
现在才知道它有什么用~~
谢 fulcrumtian教了我~~