来自lxcc(虫子|需要点勇气和信心) 的帖子 不用写控件,可以用一个如下的代码: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 '*****************************************************************
当输入一个字符时就判断其ASCII的范围!!
不用写控件,可以用一个如下的代码: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
'*****************************************************************