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