Option Explicit
Private Sub Text1_KeyPress(KeyAscii As Integer) Dim Pos As Integer
If (KeyAscii < Asc("0") Or KeyAscii > Asc("9")) And KeyAscii <> Asc(".") And KeyAscii <> 8 Then KeyAscii = 0 End If Pos = InStr(Text1.Text, ".") If Pos <> 0 And KeyAscii <> 8 Then If KeyAscii = Asc(".") Then KeyAscii = 0 Else If Len(Mid(Text1.Text, Pos)) > 2 And Text1.SelStart >= Len(Text1.Text) - 2 Then KeyAscii = 0 End If End If End If End Sub 这个代码只针对键盘输入起作用,如果要防止Ctrl+V 或鼠标右键粘贴还要另加代码
天津网站建设 http://www./