Private Sub Text1_KeyPress(KeyAscii As Integer) If Not (KeyAscii >= vbKey0 And KeyAscii <= vbKey9 Or KeyAscii = vbKeyDelete Or KeyAscii = vbKeyBack) Then KeyAscii = 0 End If End Sub
API版,嘿嘿~~Option ExplicitPrivate Declare Function SetWindowLong Lib "user32.dll" Alias "SetWindowLongA" ( _ ByVal hwnd As Long, _ ByVal nIndex As Long, _ ByVal dwNewLong As Long) As Long Private Declare Function GetWindowLong Lib "user32.dll" Alias "GetWindowLongA" ( _ ByVal hwnd As Long, _ ByVal nIndex As Long) As Long
Private Const GWL_STYLE As Long = (-16) Private Const ES_NUMBER As Long = &H2000&Private Sub Form_Load() Dim lngHwnd As Long Dim lngReturn As Long
'使文本框只能输入数字(0~9) lngHwnd = GetWindowLong(Text1.hwnd, GWL_STYLE) lngReturn = SetWindowLong(Text1.hwnd, GWL_STYLE, lngHwnd Or ES_NUMBER) End Sub
'ONLY INPUT NUMBER Public Sub NUMInput(KeyAscii As Integer) If KeyAscii = 8 Then Exit Sub If Not (KeyAscii >= 48 And KeyAscii <= 57) Then 'ONly input number KeyAscii = 0 Exit Sub End If End Sub
用IsNumeric() Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer) aa = Text1.Text If Not IsNumeric(Text1.Text) Then MsgBox "只能输入数字" Text1.Text = "" Text1.Refresh Else Text1.Text = aa End IfEnd Sub
在文本框的Change事件中加入: if IsNumeric(text1.text)=true then 输入的为数字 else 输入的不为数字 endif
Dim flg As Boolean '判断是否允许输入小数点Private Sub Form_Load() flg = True '促使允许输入小数点 End SubPrivate Sub Text1_Change() If Not IsNumeric(Text1.Text) Then Text1.Text = "" MsgBox "只能输入数字!", vbOKOnly + vbExclamation, "错误" Exit Sub End If If (Not flg) And InStr(1, Text1.Text, ".") = 0 Then '判断小数点是否被删除,如果被删除,则允许再次输入 flg = True End If End SubPrivate Sub Text1_KeyPress(KeyAscii As Integer) If (KeyAscii < vbKey0 Or KeyAscii > vbKey9) And KeyAscii <> vbKeyBack And KeyAscii <> 46 Then KeyAscii = vbCancel End If If KeyAscii = 46 Then '如果键入小数点,则判断当前是否允许输入 If flg Then flg = False Else KeyAscii = vbCancel End If End If End Sub不知以上是否还有漏洞,请各位指点
我的方法:Public Function Numeric_Only(Bybal x As Integer) As Integer Select Case x Case 8, 9, 13, &H30 To &H39, Asc(".") '可以根据需要增减 Numeric_Only = x Case Else Numeric_Only = 0 End Select End FunctionPublic Function Hex_Only(Bybal x As Integer) As Integer Select Case x Case 8, 9, 13, &H30 To &H39, Asc("A") To Asc("F") Hex_Only = x Case Asc("a") To Asc("f") Hex_Only = x - 32 Case Else Hex_Only = 0 End Select End Function调用: Private Sub Text1_KeyPress(KeyAscii As Integer) '只能输入数字 KeyAscii = Numeric_Only(KeyAscii) End SubPrivate Sub Text2_KeyPress(KeyAscii As Integer) '只能输入十六进制数字 KeyAscii = Hex_Only(KeyAscii) End Sub
正则是真理IsNumeric判断还是有些许问题的 这个函数会把科学计数法以及十六进制的认为是数字
Public Sub TXT_change(TXTB As TextBox) Dim TXT As String TXT = TXTB.Text If TXT <> "" Then Dim Str As String Str = Right(TXT, 1) If Not (StrComp(Str, "0") <> -1 And StrComp("9", Str) <> -1) Then '输入的字符在1-9之外 If Not (StrComp(Str, "A") <> -1 And StrComp("Z", Str) <> -1) Then '输入的字符在A-Z之外 If Not (StrComp(Str, "a") <> -1 And StrComp("z", Str) <> -1) Then '输入的字符在a-z之外 MsgBox "对不起,您不能输入非法字符,请您重新输入!", vbOKOnly + vbExclamation, "温馨提示" Clipboard.SetText Left(TXTB, Len(TXT) - 1) TXTB = "" TXTB.SelText = Clipboard.GetText End If End If End If End If End Sub我以前写的一个函数,用来防止文本框中输入除字母和数字以外的,你可以自己改下
Private Sub Txt_KeyPress(KeyAscii As Integer) Select Case KeyAsciiCase 45 If Txt.SelStart = 0 Then '判断“-”是否是在开始位置 Else MsgBox "不是数字" KeyAscii = 0 End IfCase 48 To 57 Case 8 '判断是否为退格符Case 46 '判断是否为为小数点 If Txt.Text <> "" And k = 0 Then k = k + 1 Else KeyAscii = 0 End IfCase Else KeyAscii = 0End Select End Sub 感觉这样比较完整些
If Not (KeyAscii >= vbKey0 And KeyAscii <= vbKey9 Or KeyAscii = vbKeyDelete Or KeyAscii = vbKeyBack) Then
KeyAscii = 0
End If
End Sub
ByVal hwnd As Long, _
ByVal nIndex As Long, _
ByVal dwNewLong As Long) As Long
Private Declare Function GetWindowLong Lib "user32.dll" Alias "GetWindowLongA" ( _
ByVal hwnd As Long, _
ByVal nIndex As Long) As Long
Private Const GWL_STYLE As Long = (-16)
Private Const ES_NUMBER As Long = &H2000&Private Sub Form_Load()
Dim lngHwnd As Long
Dim lngReturn As Long
'使文本框只能输入数字(0~9)
lngHwnd = GetWindowLong(Text1.hwnd, GWL_STYLE)
lngReturn = SetWindowLong(Text1.hwnd, GWL_STYLE, lngHwnd Or ES_NUMBER)
End Sub
Public Sub NUMInput(KeyAscii As Integer)
If KeyAscii = 8 Then Exit Sub
If Not (KeyAscii >= 48 And KeyAscii <= 57) Then 'ONly input number
KeyAscii = 0
Exit Sub
End If
End Sub
Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
aa = Text1.Text
If Not IsNumeric(Text1.Text) Then
MsgBox "只能输入数字"
Text1.Text = ""
Text1.Refresh
Else
Text1.Text = aa
End IfEnd Sub
if IsNumeric(text1.text)=true then
输入的为数字
else
输入的不为数字
endif
flg = True '促使允许输入小数点
End SubPrivate Sub Text1_Change()
If Not IsNumeric(Text1.Text) Then
Text1.Text = ""
MsgBox "只能输入数字!", vbOKOnly + vbExclamation, "错误"
Exit Sub
End If
If (Not flg) And InStr(1, Text1.Text, ".") = 0 Then '判断小数点是否被删除,如果被删除,则允许再次输入
flg = True
End If
End SubPrivate Sub Text1_KeyPress(KeyAscii As Integer)
If (KeyAscii < vbKey0 Or KeyAscii > vbKey9) And KeyAscii <> vbKeyBack And KeyAscii <> 46 Then
KeyAscii = vbCancel
End If
If KeyAscii = 46 Then '如果键入小数点,则判断当前是否允许输入
If flg Then
flg = False
Else
KeyAscii = vbCancel
End If
End If
End Sub不知以上是否还有漏洞,请各位指点
Select Case x
Case 8, 9, 13, &H30 To &H39, Asc(".") '可以根据需要增减
Numeric_Only = x
Case Else
Numeric_Only = 0
End Select
End FunctionPublic Function Hex_Only(Bybal x As Integer) As Integer
Select Case x
Case 8, 9, 13, &H30 To &H39, Asc("A") To Asc("F")
Hex_Only = x
Case Asc("a") To Asc("f")
Hex_Only = x - 32
Case Else
Hex_Only = 0
End Select
End Function调用:
Private Sub Text1_KeyPress(KeyAscii As Integer)
'只能输入数字
KeyAscii = Numeric_Only(KeyAscii)
End SubPrivate Sub Text2_KeyPress(KeyAscii As Integer)
'只能输入十六进制数字
KeyAscii = Hex_Only(KeyAscii)
End Sub
这个函数会把科学计数法以及十六进制的认为是数字
Dim TXT As String
TXT = TXTB.Text
If TXT <> "" Then
Dim Str As String
Str = Right(TXT, 1)
If Not (StrComp(Str, "0") <> -1 And StrComp("9", Str) <> -1) Then '输入的字符在1-9之外
If Not (StrComp(Str, "A") <> -1 And StrComp("Z", Str) <> -1) Then '输入的字符在A-Z之外
If Not (StrComp(Str, "a") <> -1 And StrComp("z", Str) <> -1) Then '输入的字符在a-z之外
MsgBox "对不起,您不能输入非法字符,请您重新输入!", vbOKOnly + vbExclamation, "温馨提示"
Clipboard.SetText Left(TXTB, Len(TXT) - 1)
TXTB = ""
TXTB.SelText = Clipboard.GetText
End If
End If
End If
End If
End Sub我以前写的一个函数,用来防止文本框中输入除字母和数字以外的,你可以自己改下
Select Case KeyAsciiCase 45
If Txt.SelStart = 0 Then '判断“-”是否是在开始位置
Else
MsgBox "不是数字"
KeyAscii = 0
End IfCase 48 To 57
Case 8 '判断是否为退格符Case 46 '判断是否为为小数点
If Txt.Text <> "" And k = 0 Then
k = k + 1
Else
KeyAscii = 0
End IfCase Else
KeyAscii = 0End Select
End Sub
感觉这样比较完整些