'在窗体上放Text2、Text1Option ExplicitSub TestKey(KeyAscii) '测试0-9,BS,DEL,小数点,- If (KeyAscii < 48 Or KeyAscii > 57) And (KeyAscii <> 46) And (KeyAscii <> 8) And (KeyAscii <> 13) And KeyAscii <> 45 Then Beep KeyAscii = 0 End If End Sub Private Sub Text1_KeyPress(KeyAscii As Integer) TestKey KeyAscii End Sub'只能输入等于等于○的正数 Sub TestKey_Positive_Not_Point(KeyAscii) '测试0-9,BS,DEL,- If (KeyAscii < 48 Or KeyAscii > 57) And (KeyAscii <> 8) And (KeyAscii <> 13) Then Beep KeyAscii = 0 End If End SubPrivate Sub Text2_KeyPress(KeyAscii As Integer) TestKey_Positive_Not_Point KeyAscii End Sub
Private Sub Text1_Validate(Cancel As Boolean) if Asc ("Text2.text")<48 or Asc ("Text2.text")>57 then Cancel=true End Sub
解决办法如下:Private Sub Text1_Change() If IsNumeric(Text1.Text) = False Then SendKeys "{BackSpace}" End If End Sub
Private Sub txtFee_KeyPress(KeyAscii As Integer) Dim strNumbers As String strNumbers = "1234567890" + Chr(8) + Chr(46)
If InStr(strNumbers, Chr(KeyAscii)) = 0 Then KeyAscii = 0 End If End Sub
Private Sub Text1_Validate(Cancel As Boolean) for i=1 to len(text) if Asc (mid (text,i,1))<48 or Asc (mid (text,i,1))>57 then Cancel=true next End Sub
Private Sub text1_KeyPress(KeyAscii As Integer) If KeyAscii < Asc("0") Or KeyAscii > Asc("9") Then KeyAscii = 0
Private Sub txtAR_KeyPress(KeyAscii As Integer) Select Case KeyAscii Case 48 To 57: KeyAscii = KeyAscii Case 46: '小数点 If intj = 0 Then KeyAscii = KeyAscii intj = 1 Else KeyAscii = 0 End If Case 8: KeyAscii = KeyAscii Case 9: KeyAscii = KeyAscii Case Else KeyAscii = 0 End Select End Sub
Public Function NumbersOnly(tBox As TextBox) Dim DefaultStyle As Long DefaultStyle = GetWindowLong(tBox.hWnd, GWL_STYLE) NumbersOnly = SetWindowLong(tBox.hWnd, GWL_STYLE, DefaultStyle Or ES_NUMBER) End FunctionPublic Function UpperCaseOnly(tBox As TextBox)Dim DefaultStyle As Long DefaultStyle = GetWindowLong(tBox.hWnd, GWL_STYLE) UpperCaseOnly = SetWindowLong(tBox.hWnd, GWL_STYLE, DefaultStyle Or ES_UPPERCASE)End Function Public Function LowerCaseOnly(tBox As TextBox) Dim DefaultStyle As Long DefaultStyle = GetWindowLong(tBox.hWnd, GWL_STYLE) LowerCaseOnly = SetWindowLong(tBox.hWnd, GWL_STYLE, DefaultStyle Or ES_LOWERCASE) End Function
Public Function sffunLimitNumber(ByVal IntVal As Integer) As Integer '-------------------1------------------- '目 的:只允许在文本框内输入数字、退格、删除及回车键 '输 入:ByVal IntVal As Integer,任意的键值 '被传递值:无 '返 回 值:过滤后的键值 '输 出:无 '注 解: '用 法:在文本框的KeyPress事件中输入KeyAscii = sffunLimitNumber(KeyAscii)即可 '修 订 版: '-------------------1------------------- If (IntVal <> vbKeyDelete) _ And (IntVal <> vbKeyBack) _ And (IntVal <> 13) _ And (IntVal < 48 Or IntVal > 57) Then IntVal = 0 End If sffunLimitNumber = IntValEnd Function
'测试0-9,BS,DEL,小数点,-
If (KeyAscii < 48 Or KeyAscii > 57) And (KeyAscii <> 46) And (KeyAscii <> 8) And (KeyAscii <> 13) And KeyAscii <> 45 Then
Beep
KeyAscii = 0
End If
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
TestKey KeyAscii
End Sub'只能输入等于等于○的正数
Sub TestKey_Positive_Not_Point(KeyAscii)
'测试0-9,BS,DEL,-
If (KeyAscii < 48 Or KeyAscii > 57) And (KeyAscii <> 8) And (KeyAscii <> 13) Then
Beep
KeyAscii = 0
End If
End SubPrivate Sub Text2_KeyPress(KeyAscii As Integer)
TestKey_Positive_Not_Point KeyAscii
End Sub
if Asc ("Text2.text")<48 or Asc ("Text2.text")>57 then Cancel=true
End Sub
If IsNumeric(Text1.Text) = False Then
SendKeys "{BackSpace}"
End If
End Sub
Dim strNumbers As String
strNumbers = "1234567890" + Chr(8) + Chr(46)
If InStr(strNumbers, Chr(KeyAscii)) = 0 Then
KeyAscii = 0
End If
End Sub
Private Sub Text1_Validate(Cancel As Boolean)
for i=1 to len(text)
if Asc (mid (text,i,1))<48 or Asc (mid (text,i,1))>57 then Cancel=true
next
End Sub
If KeyAscii < Asc("0") Or KeyAscii > Asc("9") Then
KeyAscii = 0
End If
End Sub
Select Case KeyAscii
Case 48 To 57:
KeyAscii = KeyAscii
Case 46: '小数点
If intj = 0 Then
KeyAscii = KeyAscii
intj = 1
Else
KeyAscii = 0
End If
Case 8:
KeyAscii = KeyAscii
Case 9:
KeyAscii = KeyAscii
Case Else
KeyAscii = 0
End Select
End Sub
Dim DefaultStyle As Long
DefaultStyle = GetWindowLong(tBox.hWnd, GWL_STYLE)
NumbersOnly = SetWindowLong(tBox.hWnd, GWL_STYLE, DefaultStyle Or ES_NUMBER)
End FunctionPublic Function UpperCaseOnly(tBox As TextBox)Dim DefaultStyle As Long
DefaultStyle = GetWindowLong(tBox.hWnd, GWL_STYLE)
UpperCaseOnly = SetWindowLong(tBox.hWnd, GWL_STYLE, DefaultStyle Or ES_UPPERCASE)End Function
Public Function LowerCaseOnly(tBox As TextBox)
Dim DefaultStyle As Long
DefaultStyle = GetWindowLong(tBox.hWnd, GWL_STYLE)
LowerCaseOnly = SetWindowLong(tBox.hWnd, GWL_STYLE, DefaultStyle Or ES_LOWERCASE)
End Function
'-------------------1-------------------
'目 的:只允许在文本框内输入数字、退格、删除及回车键
'输 入:ByVal IntVal As Integer,任意的键值
'被传递值:无
'返 回 值:过滤后的键值
'输 出:无
'注 解:
'用 法:在文本框的KeyPress事件中输入KeyAscii = sffunLimitNumber(KeyAscii)即可
'修 订 版:
'-------------------1-------------------
If (IntVal <> vbKeyDelete) _
And (IntVal <> vbKeyBack) _
And (IntVal <> 13) _
And (IntVal < 48 Or IntVal > 57) Then
IntVal = 0
End If
sffunLimitNumber = IntValEnd Function