Private Sub Text1_KeyPress(KeyAscii As Integer) If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0 End IfEnd Sub
Private Sub Text1_KeyPress(KeyAscii As Integer) Const xStr As String = "0123456789" KeyAscii = IIf(InStr(xStr, Chr(KeyAscii)), KeyAscii, 0) End Sub
大家都忽略一个问题:小数点,Enter键,删除键,Tab键都不能使用,请大家继续完成我的问题
補充: 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了麼?
删除键和Del键还是不能用,请你再写一点好吗?
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 FunctionPrivate Sub Text1_KeyPress(KeyAscii As Integer) If In_single(KeyAscii) = False Then KeyAscii = 0 End If 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
再次修改: If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 46 And KeyAscii <> 8 Then KeyAscii = 0 End If呵呵,這樣沒問題了吧?
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
再加一句: If KeyAscii = 46 And InStr(Text1.Text, ".") > 0 Then KeyAscii = 0 End If因為只能接受小數點一次。
KeyAscii = 0
End IfEnd Sub
Const xStr As String = "0123456789"
KeyAscii = IIf(InStr(xStr, Chr(KeyAscii)), KeyAscii, 0)
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了麼?
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 FunctionPrivate Sub Text1_KeyPress(KeyAscii As Integer)
If In_single(KeyAscii) = False Then
KeyAscii = 0
End If
End Sub
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
KeyAscii = 0
End If呵呵,這樣沒問題了吧?
Dim xStr As String
xStr = "0123456789." & Chr(8) & Chr(13)
KeyAscii = IIf(InStr(xStr, Chr(KeyAscii)), KeyAscii, 0)
End Sub
If KeyAscii = 46 And InStr(Text1.Text, ".") > 0 Then
KeyAscii = 0
End If因為只能接受小數點一次。