Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer) If KeyCode < 48 Or KeyCode > 57 Then Exit Sub End If End Sub
'***************************************************************** '文本框只能输入数字Public Sub InputNumeric(KeyAscii As Integer, txtItem As TextBox) Select Case KeyAscii Case Asc("-") '允许负数 If txtItem(i).SelStart = 0 Then If Left(txtItem(i).Text, 1) = "-" Then KeyAscii = 0 Beep End If Else KeyAscii = 0 Beep End If Case 8 '无变化,退格键不屏蔽 Case Asc(" ") '32 If txtItem(i).SelLength = 0 Then KeyAscii = 0 End If Case Asc(".") '46 '允许小数点 If InStr(txtItem(i).Text, ".") Then KeyAscii = 0 End If Case Is < Asc(0) '48 KeyAscii = 0 Case Is > Asc(9) '57 KeyAscii = 0 End Select End Sub '*****************************************************************
用的时候只要指明 InputNumeric keyascii,yourtextname
If Not IsNumeric(Text1(5).Text) Then MsgBox ("必须为数字哦") Exit Sub End If
If KeyCode < 48 Or KeyCode > 57 Then
Exit Sub
End If
End Sub
'文本框只能输入数字Public Sub InputNumeric(KeyAscii As Integer, txtItem As TextBox)
Select Case KeyAscii
Case Asc("-") '允许负数
If txtItem(i).SelStart = 0 Then
If Left(txtItem(i).Text, 1) = "-" Then
KeyAscii = 0
Beep
End If
Else
KeyAscii = 0
Beep
End If
Case 8
'无变化,退格键不屏蔽
Case Asc(" ") '32
If txtItem(i).SelLength = 0 Then
KeyAscii = 0
End If
Case Asc(".") '46 '允许小数点
If InStr(txtItem(i).Text, ".") Then
KeyAscii = 0
End If
Case Is < Asc(0) '48
KeyAscii = 0
Case Is > Asc(9) '57
KeyAscii = 0
End Select
End Sub
'*****************************************************************
InputNumeric keyascii,yourtextname
MsgBox ("必须为数字哦")
Exit Sub
End If