Private Sub Text1_KeyPress(KeyAscii As Integer) InputNumeric KeyAscii, Text1 End Sub'***************************************************************** '文本框只能输入数字 Public Sub InputNumeric(KeyAscii As Integer, txtItem As TextBox) Select Case KeyAscii Case Asc("-") '允许负数 If txtItem.SelStart = 0 Then If Left(txtItem.Text, 1) = "-" Then KeyAscii = 0 Beep End If Else KeyAscii = 0 Beep End If Case 8 '无变化,退格键不屏蔽 Case Asc(" ") '32 If txtItem.SelLength = 0 Then KeyAscii = 0 End If Case Asc(".") '46 '允许小数点 If InStr(txtItem.Text, ".") Then KeyAscii = 0 End If Case Is < Asc(0) '48 KeyAscii = 0 Case Is > Asc(9) '57 KeyAscii = 0 End Select End Sub '*****************************************************************
If (KeyAscii < 48 Or KeyAscii > 57) Then If KeyAscii <> 8 And _ KeyAscii <> 46 And _ KeyAscii <> 45 Then KeyAscii = 0 End If End If 就这点就可以啦,写程序嘛,只要效果一样,还是简一点的好
把代码放在文本框的Keypress事件中
太复杂了 还是用isnumeric吧:)
Dim MyNum, MyChkNum MyChkNum = IsNumeric(MyNum) If MyChkNum<>True Then KeyAscii=0
InputNumeric KeyAscii, Text1
End Sub'*****************************************************************
'文本框只能输入数字
Public Sub InputNumeric(KeyAscii As Integer, txtItem As TextBox)
Select Case KeyAscii
Case Asc("-") '允许负数
If txtItem.SelStart = 0 Then
If Left(txtItem.Text, 1) = "-" Then
KeyAscii = 0
Beep
End If
Else
KeyAscii = 0
Beep
End If
Case 8
'无变化,退格键不屏蔽
Case Asc(" ") '32
If txtItem.SelLength = 0 Then
KeyAscii = 0
End If
Case Asc(".") '46 '允许小数点
If InStr(txtItem.Text, ".") Then
KeyAscii = 0
End If
Case Is < Asc(0) '48
KeyAscii = 0
Case Is > Asc(9) '57
KeyAscii = 0
End Select
End Sub
'*****************************************************************
If KeyAscii <> 8 And _
KeyAscii <> 46 And _
KeyAscii <> 45 Then
KeyAscii = 0
End If
End If
就这点就可以啦,写程序嘛,只要效果一样,还是简一点的好
还是用isnumeric吧:)
MyChkNum = IsNumeric(MyNum)
If MyChkNum<>True Then KeyAscii=0