Private Sub Text1_KeyPress(KeyAscii As Integer) If KeyAscii > Asc(9) Or KeyAscii < (0) Then KeyAscii = 0 End IfEnd Sub
如果只为了避免输入错误.我不同意上楼的做法.你完全可以用: Private Sub Text1_Validate(Cancel As Boolean) If Not IsNumeric(Text1) Then Cancel = True MsgBox "error" End If End Sub
Private Sub text1_KeyPress(Index As Integer, KeyAscii As Integer) If (KeyAscii < 48 Or KeyAscii > 57) And (KeyAscii <> 8 And KeyAscii <> 45) Then KeyAscii = 0 End Sub
1. 禁止使用键盘向文本框内输入非法字符(你的需要是否限定0到9这10个数字?) 上面已经给出代码 2. 禁止用户使用右键菜单的粘贴菜单项 3. 禁止用户使用 Ctrl + V 注意这三项就可以了 第二和第三点最终会发出 WM_PASTE 消息给文本框,然后再发出 WM_SETTEXT ... 如果截获 WM_PASTE 消息是可以解决第二个和第二个问题 不过右键菜单的粘贴项却还为可用的... 是要为了禁止粘贴而把整个右键菜单给屏蔽掉呢... 还是只把粘贴这个项给灰掉...Public Sub Hook(ByVal hWnd As Long) PrevProc = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf WindowProc) End SubPublic Sub UnHook(ByVal hWnd As Long) SetWindowLong hWnd, GWL_WNDPROC, PrevProc End SubPublic Function WindowProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long If uMsg = WM_PASTE Then WindowProc = 0 Exit Function ElseIf uMsg = WM_CONTEXTMENU Then WindowProc = 0 Exit Function End If WindowProc = CallWindowProc(PrevProc, hWnd, uMsg, wParam, lParam) End Function
If Not IsNumeric(Text1.text) Then 使用isnumeric函数
boywang(大力水手) 的再加上: text1_keydown(..) if shift=vbctrl then text1.locked=true text1_keyup (..) if text1.locked then text1.locked=false 综合以上各位所说来看,Intelement(桂子) 的钩子回调技术最有效,他可以屏蔽右键菜单
同意用isNumeric(),在keypress事件中检测key=13 回车时。
我觉得还是hhyttppd(123456)的方法好 如果只为了避免输入错误.我不同意上楼的做法.你完全可以用: Private Sub Text1_Validate(Cancel As Boolean) If Not IsNumeric(Text1) Then Cancel = True MsgBox "error" End If End Sub如果全部屏蔽,用户在中文状态下输不了小数点,且一般用户不知道是这个问题,反尔会认为是软件不好用
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
这样写绝对只能输入数字: 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 txtFee_KeyDown(KeyCode As Integer, Shift As Integer) if (KeyCode >= vbKey0 And KeyCode <= vbKey9) Or (KeyCode >= vbKeyNumpad0 And KeyCode <= vbKeyNumpad9) then elseif KeyCode = vbkeyback or keycode=vbkeyreturn then else msgbox "请输入数字!" endif end sub 这样就解决了,用户是用数字小键盘输入,还是用主键盘上的数字输入的问题. 但是,要用了CTRL+V粘贴了字符的话,就可以用下面的方法解决. private sub txtFee__LostFocus() if not isnumeric(trim(txtFee.text)) then msgbox "请输入数字!" endif end sub
Option Explicit'######################################################### '#KeyAscii按键 '#InputNum可取 真(True)或假(False) '# True 可输入整数 '# False 可输入小数 '# '#########################################################Function CheckNum(str As String, KeyAscii As Integer, InputNum As Boolean)If InputNum Then If InStr(str, ".") And KeyAscii = 46 Then KeyAscii = 0 Exit Function End If If Not (IsNumeric(Chr(KeyAscii)) Or KeyAscii = 8) Then KeyAscii = 0 Else If Len(str) = 0 And KeyAscii = 46 Then KeyAscii = 0 Exit Function End If If InStr(str, ".") And KeyAscii = 46 Then KeyAscii = 0 Exit Function End If If Not (IsNumeric(Chr(KeyAscii)) Or KeyAscii = 8 Or KeyAscii = 46) Then KeyAscii = 0 End If End FunctionPrivate Sub Text1_KeyPress(KeyAscii As Integer) CheckNum Text1.Text, KeyAscii, False End Sub Private Sub Text2_KeyPress(KeyAscii As Integer) CheckNum Text2.Text, KeyAscii, True End Sub
Private Sub Text1_Validate(Cancel As Boolean) If Not IsNumeric(Text1.text) Then msgbox "输入有误,请输入数字" ElseIf msbbox "正确" End If End Sub我也认为用IsNumeric比较简单
并不等效。 Validate + IsNumeric 是事后检查 KeyPress 是事先限制就我来说,不喜欢输入了一大串后弹出一个对话框说“你错了”。我的方法: Private Sub Text1_KeyPress(KeyAscii As Integer) KeyAscii = NumberOnly(KeyAscii) End Subpublid function NumberOnly(byval x as integer)as integer select case x case 8,9,13,&h31 to &h39 NumberOnly=x case else NumberOnly=0 end select end function你还可以仿此,写出限于16进值数或纯阿拉伯数字的函数,而且支持回删、跳格,回车键。 在任何需要限制的文本框的keypress事件中加上一句调用就可以了,而且是控件无关的,可以复制。
上面函数如果要支持小数点: case 8,9,13,&h2e,&h31 to &h39 NumberOnly=x
Private Sub Text1_KeyPress(KeyAscii As Integer) if chr(KeyAscii) like (0-9) then KeyAscii=KeyAscii else KeyAscii=0 end if end sub
If KeyAscii >= &H20 And (KeyAscii < &H30 Or KeyAscii > &H39) Then
KeyAscii = 0
End If
End Sub
却还可以使用 Ctrl + V 来粘贴非数字字符
所以啊
还得屏蔽 Ctrl + V
http://expert.csdn.net/Expert/FAQ/FAQ_Index.asp?id=137587
If KeyAscii > Asc(9) Or KeyAscii < (0) Then
KeyAscii = 0
End IfEnd Sub
Private Sub Text1_Validate(Cancel As Boolean)
If Not IsNumeric(Text1) Then
Cancel = True
MsgBox "error"
End If
End Sub
If (KeyAscii < 48 Or KeyAscii > 57) And (KeyAscii <> 8 And KeyAscii <> 45) Then KeyAscii = 0
End Sub
上面已经给出代码
2. 禁止用户使用右键菜单的粘贴菜单项
3. 禁止用户使用 Ctrl + V
注意这三项就可以了
第二和第三点最终会发出 WM_PASTE 消息给文本框,然后再发出 WM_SETTEXT ...
如果截获 WM_PASTE 消息是可以解决第二个和第二个问题
不过右键菜单的粘贴项却还为可用的...
是要为了禁止粘贴而把整个右键菜单给屏蔽掉呢...
还是只把粘贴这个项给灰掉...Public Sub Hook(ByVal hWnd As Long)
PrevProc = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf WindowProc)
End SubPublic Sub UnHook(ByVal hWnd As Long)
SetWindowLong hWnd, GWL_WNDPROC, PrevProc
End SubPublic Function WindowProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If uMsg = WM_PASTE Then
WindowProc = 0
Exit Function
ElseIf uMsg = WM_CONTEXTMENU Then
WindowProc = 0
Exit Function
End If
WindowProc = CallWindowProc(PrevProc, hWnd, uMsg, wParam, lParam)
End Function
使用isnumeric函数
text1_keydown(..)
if shift=vbctrl then text1.locked=true
text1_keyup (..)
if text1.locked then text1.locked=false
综合以上各位所说来看,Intelement(桂子) 的钩子回调技术最有效,他可以屏蔽右键菜单
如果只为了避免输入错误.我不同意上楼的做法.你完全可以用:
Private Sub Text1_Validate(Cancel As Boolean)
If Not IsNumeric(Text1) Then
Cancel = True
MsgBox "error"
End If
End Sub如果全部屏蔽,用户在中文状态下输不了小数点,且一般用户不知道是这个问题,反尔会认为是软件不好用
'-------------------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
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
if (KeyCode >= vbKey0 And KeyCode <= vbKey9) Or (KeyCode >= vbKeyNumpad0 And KeyCode <= vbKeyNumpad9) then
elseif KeyCode = vbkeyback or keycode=vbkeyreturn then
else
msgbox "请输入数字!"
endif
end sub
这样就解决了,用户是用数字小键盘输入,还是用主键盘上的数字输入的问题.
但是,要用了CTRL+V粘贴了字符的话,就可以用下面的方法解决.
private sub txtFee__LostFocus()
if not isnumeric(trim(txtFee.text)) then
msgbox "请输入数字!"
endif
end sub
'#KeyAscii按键
'#InputNum可取 真(True)或假(False)
'# True 可输入整数
'# False 可输入小数
'#
'#########################################################Function CheckNum(str As String, KeyAscii As Integer, InputNum As Boolean)If InputNum Then
If InStr(str, ".") And KeyAscii = 46 Then
KeyAscii = 0
Exit Function
End If
If Not (IsNumeric(Chr(KeyAscii)) Or KeyAscii = 8) Then KeyAscii = 0
Else
If Len(str) = 0 And KeyAscii = 46 Then
KeyAscii = 0
Exit Function
End If
If InStr(str, ".") And KeyAscii = 46 Then
KeyAscii = 0
Exit Function
End If
If Not (IsNumeric(Chr(KeyAscii)) Or KeyAscii = 8 Or KeyAscii = 46) Then KeyAscii = 0
End If
End FunctionPrivate Sub Text1_KeyPress(KeyAscii As Integer)
CheckNum Text1.Text, KeyAscii, False
End Sub
Private Sub Text2_KeyPress(KeyAscii As Integer)
CheckNum Text2.Text, KeyAscii, True
End Sub
If Not IsNumeric(Text1.text) Then
msgbox "输入有误,请输入数字"
ElseIf
msbbox "正确"
End If
End Sub我也认为用IsNumeric比较简单
Validate + IsNumeric 是事后检查
KeyPress 是事先限制就我来说,不喜欢输入了一大串后弹出一个对话框说“你错了”。我的方法:
Private Sub Text1_KeyPress(KeyAscii As Integer)
KeyAscii = NumberOnly(KeyAscii)
End Subpublid function NumberOnly(byval x as integer)as integer
select case x
case 8,9,13,&h31 to &h39
NumberOnly=x
case else
NumberOnly=0
end select
end function你还可以仿此,写出限于16进值数或纯阿拉伯数字的函数,而且支持回删、跳格,回车键。
在任何需要限制的文本框的keypress事件中加上一句调用就可以了,而且是控件无关的,可以复制。
case 8,9,13,&h2e,&h31 to &h39
NumberOnly=x
if chr(KeyAscii) like (0-9) then
KeyAscii=KeyAscii
else
KeyAscii=0
end if
end sub
msgbox(----)