包括正负小数的:
Text1.Locked=True
Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 46 Then
If Text1.SelText <> "" Then
Text1.SelText = ""
ElseIf Text1.SelStart < Len(Text1.Text) Then
Text1.SelLength = 1
Text1.SelText = ""
End If
End If
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 48 To 57
Text1.SelText = Chr(KeyAscii)
Case 8
If Text1.SelText <> "" Then
Text1.SelText = ""
ElseIf Text1.SelStart > 0 Then
Text1.SelStart = Text1.SelStart - 1
Text1.SelLength = 1
Text1.SelText = ""
End If
Case 45
If Text1.SelStart = 0 And InStr(Text1.SelLength + 1, Text1, "-") = 0 Then
Text1.SelText = "-"
End If
Case 46
If (InStr(Text1.Text, ".") = 0 Or InStr(Text1.SelText, ".") > 0) And (Text1.SelStart _
>= InStr(Text1.Text, "-") Or InStr(Text1.SelText, "-") > 0) Then
Text1.SelText = "."
End If
End Select
End Sub