Private Sub Text1_Change() If Not ValidateNumeric(Text1.Text) ThenText1.Text = ""End If End Sub Private Function ValidateNumeric(strText As String) As BooleanValidateNumeric = CBool(strText = "" Or strText = "-" Or strText = "+" Or strText = "." Or IsNumeric(strText))End Function
private sub text1_validate() if text1<>"" then if not isnumeric(text1) then msgbox "不是数字" end if end sub
private sub text1_lostfocus() if text1.text<>"" then if not isnumeric(text1.text) then msgbox "请输入数值型数据!",48,"提示" text1.setfocus end if end if end sub
private sub text1_validate() if not isnumeric(text1.text) then cancel=true msgbox"请输入数字",,"数字" end if end sub 数字那地方就是你text1前面label几的caption
我建议你在输入值的时候就判断输入的是否为数字,不必要在TextBox失去焦点的时候再判断是否为数字。对应的是它的KeyPress事件: Private Sub Text1_KeyPress(KeyAscii As Integer) If KeyAscii <> 8 And (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 13 Then KeyAscii = 0 End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer) Dim Numbers As String Numbers = "1234567890" + Chr(8) + Chr(46) If InStr(Numbers, Chr(KeyAscii)) = 0 Then KeyAscii = 0 End If End Sub
If Not ValidateNumeric(Text1.Text) ThenText1.Text = ""End If
End Sub
Private Function ValidateNumeric(strText As String) As BooleanValidateNumeric = CBool(strText = "" Or strText = "-" Or strText = "+" Or strText = "." Or IsNumeric(strText))End Function
if text1<>"" then
if not isnumeric(text1) then msgbox "不是数字"
end if
end sub
if text1.text<>"" then
if not isnumeric(text1.text) then
msgbox "请输入数值型数据!",48,"提示"
text1.setfocus
end if
end if
end sub
if not isnumeric(text1.text) then
cancel=true
msgbox"请输入数字",,"数字"
end if
end sub
数字那地方就是你text1前面label几的caption
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii <> 8 And (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 13 Then KeyAscii = 0
End Sub
Dim Numbers As String
Numbers = "1234567890" + Chr(8) + Chr(46)
If InStr(Numbers, Chr(KeyAscii)) = 0 Then
KeyAscii = 0
End If
End Sub