较完美的,先贴出来,方便以后Copy来用,(下面代码依然未考虑科学计数法问题)
Select Case KeyAscii
Case 8
Case 13
Case 45
If Text0(Index).SelStart <> 0 Or (InStr(Text0(Index), "-") And Text0(Index).SelLength <> Len(Text0(Index))) > 0 Then KeyAscii = 0 '如果光标不在第一位,则取消负号的录入
Case 46
If InStr(Text0(Index), ".") > 0 Then
KeyAscii = 0 '只接收一个小数点“.”
ElseIf Text0(Index).SelStart = 0 Then
KeyAscii = 0
Text0(Index) = "0." & Text0(Index) '当文本框中没有任何字符或只有负号时输入小数点,则自动在小数点前加上“0”
Text0(Index).SelStart = Len(Text0(Index)) '将光标设在文本之后
ElseIf Left(Text0(Index), 1) = "-" And Text0(Index).SelStart = 1 Then
KeyAscii = 0
Text0(Index) = "-0." & Right(Text0(Index), Len(Text0(Index)) - 1)
Text0(Index).SelStart = Len(Text0(Index))
End If
Case Is < 48
KeyAscii = 0
Case 48
If Left(Text0(Index), 1) = "0" And Text0(Index).SelStart = 1 Then KeyAscii = 0
If Left(Text0(Index), 2) = "-0" And Text0(Index).SelStart = 2 Then KeyAscii = 0
Case Is > 57
KeyAscii = 0
End Select
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii <> 8 And KeyAscii <> 13 And KeyAscii <> 45 And KeyAscii <> 46 Then '例外项(即允许退格键、回车键、负号键、小数点的输入)
If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0 '取消非数字字符的输入
Else
If KeyAscii = 45 Then If Text1.SelStart <> 0 And InStr(Text1, "-") = 0 Then KeyAscii = 0 '如果光标不在第一位,则取消负号的录入
If KeyAscii = 46 Then
If InStr(Text1, ".") > 0 Then KeyAscii = 0 '只接收一个小数点“.”
If Text1 = "" Then '当文本框中没有任何字符或只有负号时输入小数点,则自动在小数点前加上“0”
Text1 = "0."
KeyAscii = 0
Text1.SelStart = Len(Text1) '将光标设在文本之后
End If
If Text1 = "-" Then
Text1 = "-0."
KeyAscii = 0
Text1.SelStart = Len(Text1)
End If
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii <> 8 And KeyAscii <> 13 And KeyAscii <> 45 And KeyAscii <> 46 Then '例外项(即允许退格键、回车键、负号键、小数点的输入)
If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0 '取消非数字字符的输入
Else
If KeyAscii = 45 Then If Text1.SelStart <> 0 Then KeyAscii = 0 '如果光标不在第一位,则取消负号的录入
If KeyAscii = 46 Then
If InStr(Text1, ".") > 0 Then KeyAscii = 0 '只接收一个小数点“.”
If Text1 = "" Then '当文本框中没有任何字符或只有负号时输入小数点,则自动在小数点前加上“0”
Text1 = "0."
KeyAscii = 0
Text1.SelStart = Len(Text1) '将光标设在文本之后
End If
If Text1 = "-" Then
Text1 = "-0."
KeyAscii = 0
Text1.SelStart = Len(Text1)
End If
Private Sub Text1_KeyPress(KeyAscii As Integer)
If (Text1.SelStart <> 0 Or InStr(1, Text1.Text, "-") <> 0) And KeyAscii = 45 Then KeyAscii = 0
If InStr(1, Text1.Text, "-") <> 0 And Text1.SelStart = 0 Then KeyAscii = 0
If InStr(1, Text1.Text, ".") <> 0 And KeyAscii = 46 Then KeyAscii = 0
If (KeyAscii < 45 Or KeyAscii > 57) And KeyAscii <> 8 Then KeyAscii = 0
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii <> 8 And KeyAscii <> 13 And KeyAscii <> 45 And KeyAscii <> 46 Then
If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0
Else
If KeyAscii = 45 Then If Text1 <> "" Then KeyAscii = 0
If KeyAscii = 46 Then
If InStr(Text1, ".") > 0 Then KeyAscii = 0
If Text1 = "" Then
Text1 = "0."
KeyAscii = 0
Text1.SelStart = Len(Text1)
End If
If Text1 = "-" Then
Text1 = "-0."
KeyAscii = 0
Text1.SelStart = Len(Text1)
End If
Private Sub Text1_KeyPress(KeyAscii As Integer)
If Len(Trim(Text1.Text)) > 0 And KeyAscii = 45 Then KeyAscii = 0
If InStr(1, Text1.Text, ".") <> 0 And KeyAscii = 46 Then KeyAscii = 0
If (KeyAscii < 45 Or KeyAscii > 57) And KeyAscii <> 8 Then KeyAscii = 0
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
If (Text1.SelStart <> 0 Or InStr(1, Text1.Text, "-") <> 0) And KeyAscii = 45 Then KeyAscii = 0
If InStr(1, Text1.Text, "-") <> 0 And Text1.SelStart = 0 Then KeyAscii = 0
If InStr(1, Text1.Text, ".") <> 0 And KeyAscii = 46 Then KeyAscii = 0
If (KeyAscii < 45 Or KeyAscii > 57) And KeyAscii <> 8 Then KeyAscii = 0
End Sub
KeyAscii < 45 Or KeyAscii > 57 这些是数字的ASCII。
KeyAscii = 8可以输入退格键。
Private Sub Text1_KeyPress(KeyAscii As Integer)
If InStr(1, Text1.Text, ".") > 0 Then
haveD = True
Else
haveD = False
End If
If (haveD = True) And (KeyAscii = 46) Then
KeyAscii = 0
End If
If Text1.SelStart = 0 And InStr(1, Text1.Text, "-") > 0 Then
KeyAscii = 0
End If
If KeyAscii = 45 Then
If (InStr(1, Text1.Text, "-") = 0) And (Text1.SelStart = 0) Then
KeyAscii = 45
Else
KeyAscii = 0
End If
End If
If (KeyAscii > 57 Or KeyAscii < 48) And KeyAscii <> 46 And KeyAscii <> 45 And KeyAscii <> 13 And KeyAscii <> 8 Then
KeyAscii = 0
End If
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii <> 8 And KeyAscii <> 13 And KeyAscii <> 45 And KeyAscii <> 46 Then '例外项(即允许退格键、回车键、负号键、小数点的输入)
If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0 '取消非数字字符的输入
Else
If KeyAscii = 45 Then If Text1.SelStart <> 0 Or InStr(Text1, "-") > 0 Then KeyAscii = 0 '如果光标不在第一位,则取消负号的录入
If KeyAscii = 46 Then
If InStr(Text1, ".") > 0 Then KeyAscii = 0 '只接收一个小数点“.”
If Text1 = "" Then '当文本框中没有任何字符或只有负号时输入小数点,则自动在小数点前加上“0”
Text1 = "0."
KeyAscii = 0
Text1.SelStart = Len(Text1) '将光标设在文本之后
End If
If Text1 = "-" Then
Text1 = "-0."
KeyAscii = 0
Text1.SelStart = Len(Text1)
End If