下面一段实现了一部分:(,"-"号只能输在第一位,小数点不能输在第一位,和最后一位)---这个还未实现
Private Sub Text1_KeyPress(KeyAscii As Integer) Select Case KeyAscii
Case Asc("0") To Asc("9"), vbKeyBack
Case Asc(".")
If InStr(1, Text2.Text, ".") > 0 Then KeyAscii = 0
Case Asc("-")
If InStr(1, Text2.Text, "-") > 0 Then KeyAscii = 0
Case Else
KeyAscii = 0
End Select
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer) Select Case KeyAscii
Case Asc("0") To Asc("9"), vbKeyBack
Case Asc(".")
If InStr(1, Text2.Text, ".") > 0 Then KeyAscii = 0
Case Asc("-")
If InStr(1, Text2.Text, "-") > 0 Then KeyAscii = 0
Case Else
KeyAscii = 0
End Select
End Sub
Case Asc("0") To Asc("9"), vbKeyBack
Case Asc(".")
If InStr(1, Text1.Text, ".") > 0 Then KeyAscii = 0
Case Asc("-")
If InStr(1, Text1.Text, "-") > 0 Then KeyAscii = 0
Case Else
KeyAscii = 0
End Select
End Sub
全部Text1才对
'===== Leezo 2005-08-13
'=====功能:只能输入数字,包括负号、小数点
'=====参数:oText -- 控件对象
'===== iKey -- 按键时的 KeyAscii
'===== bPoint -- 能否输入小数点
'===== bRagetive -- 能否输入负号
'=====返回值:返回合条件的KeyAscii值
NumberOnly = iKey
Select Case iKey
Case Asc("-") '允许负数
If bNagetive = True Then
If oText.SelStart = 0 Then
If Left(oText.Text, 1) = "-" Then
NumberOnly = 0
Beep
End If
Else
NumberOnly = 0
Beep
End If
Else
NumberOnly = 0
End If
Case 8
'无变化,退格键不屏蔽
Case Asc(" ") '32
If oText.SelLength = 0 Then
NumberOnly = 0
End If
Case Asc(".") '46 '允许小数点
If bPoint = True Then
If InStr(oText.Text, ".") Then
NumberOnly = 0
End If
Else
NumberOnly = 0
End If
Case Is < Asc(0) '48
NumberOnly = 0
Case Is > Asc(9) '57
NumberOnly = 0
End Select
End FunctionPrivate Sub Text1_KeyPress(KeyAscii As Integer)
KeyAscii = NumberOnly(Text1, KeyAscii, True, True)
End Sub我在程序中都用这种方法,虽然会有些特别的情况出现,但感觉上还可以,
因为我一般都会加上: 结果 = Val(Text1.Text)
特别情况如:00.0123、-.365、-000.123如果有更好的方法,也请上发一份,好让我学学,谢谢!