Private Sub Text1_KeyPress(KeyAscii As Integer)
Dim xStr As String
xStr = "0123456789." & Chr(8) & Chr(13)
KeyAscii = IIf(InStr(xStr, Chr(KeyAscii)), KeyAscii, 0)
End Sub
private sub text1_change()
dim LastChar as string
LastChar=right(text1.text,1)
if isnumeric(LastChar)=false then
Text1.text=left(text1.text,len(text1.text)-1)
end if
end sub
Public Function In_single(KeyAscii As Integer) As Boolean
Dim ch_accept_single(20) As String
ch_accept_single(0) = "0"
ch_accept_single(1) = "1"
ch_accept_single(2) = "2"
ch_accept_single(3) = "3"
ch_accept_single(4) = "4"
ch_accept_single(5) = "5"
ch_accept_single(6) = "6"
ch_accept_single(7) = "7"
ch_accept_single(8) = "8"
ch_accept_single(9) = "9"
ch_accept_single(10) = "."
ch_accept_single(11) = "-"
ch_accept_single(12) = Chr(8)
In_single = False
For i = 0 To 12
If Chr(KeyAscii) = ch_accept_single(i) Then
In_single = True
End If
Next
End Function
Private Sub Text1_KeyPress(KeyAscii As Integer)
If In_single(KeyAscii) = False Then
KeyAscii = 0
End If
End Sub
補充:
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii < 48 Or KeyAscii > 57 Then
If KeyAscii <> 46 Then
KeyAscii = 0
End If
End If
End Sub
這樣不是OK了麼?