一
Private Sub Text1_KeyPress(KeyAscii As Integer)
If Not IsNumeric(Chr(KeyAscii)) Then KeyAscii = 0
End Sub
二
Private Sub Text1_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 48 To 57
Case 8
Case Else
Beep
MsgBox "error!"
KeyAscii = 0
End Select
三
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Const ES_NUMBER = &H2000
Private Const GWL_STYLE = -16
Private Sub ForceNumeric(Box As TextBox)
On Error Goto Catch
Dim nStyle As Long
nStyle = GetWindowLong(Box.hWnd, GWL_STYLE)
Call SetWindowLong(Box.hWnd, GWL_STYLE, nStyle Or ES_NUMBER)
Goto Finally
Catch:
Call MsgBox(Err.Description, vbCritical Or vbOKOnly, "Error")
Finally:
End Sub
private sub text1_lostfocus()
if text1.text<>"" then
if not isnumeric(text1.text) then
msgbox "请输入数值型数据!",48,"提示"
text1.setfocus
exit sub
end if
end if
end sub