小妹子请问在vb中的text中如何限制非数字的输入?(一个极弱智的问题啦)

lhh 2001-11-27 04:38:52
我在vb中的text的keypress中对以下应如何处理呀(略去非数字字符的输入)
Private Sub txtBom_KeyPress(KeyAscii As Integer)
If KeyAscii < 49 And KeyAscii > 57 Then
××××××××××××
End If
End Sub
谢谢了!
...全文
266 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
nevjiang 2002-01-28
  • 打赏
  • 举报
回复
Private Function confineinput(para_keyascii As Integer, para_confinestr As String)
Dim sconfinestr As String
sconfinestr = UCase(para_confinestr) + Chr(13) + Chr(8) + Chr(27)
If InStr(1, sconfinestr, UCase(Chr(para_keyascii)), vbTextCompare) = 0 Then
confineinput = 0
Else
confineinput = para_keyascii
End If
End Function
Private Sub Text1_KeyPress(KeyAscii As Integer)
KeyAscii = confineinput(KeyAscii, "1234567890")
End Sub
Bardo 2001-11-27
  • 打赏
  • 举报
回复
就是这样还是可以复制进去,所以最安全的是用FORM20.DLL中的TEXTBOX。
ljzhost 2001-11-27
  • 打赏
  • 举报
回复
如果在 文本中只是输入0~9
If(KeyAscii < 48 Or KeyAscii > 57 ) And KeyAscii<>8 then
KeyAscii=0
Beep
Exit Sub
End If


flyingshadow 2001-11-27
  • 打赏
  • 举报
回复
楼上-荷船-对,补充点
private sub text1_keypress(keyascii as integer)
if keyascii=8 then exit sub '退格
if keyascii=13 then
XXXXXXXXXXX '回车,通常调用过程。commandX_click
end if
if keyascii=asc(".") then exit sub '别忘了小数点哦~~
if keyascii=asc("-") then exit sub '负数啦!
if keyascii< asc("0") or keyascii > asc("9") then keyascii=0
end sub
nsdb 2001-11-27
  • 打赏
  • 举报
回复
If KeyAscii < 48 Or KeyAscii > 57 Then
If KeyAscii <> 8 And KeyAscii <> 13 Then
KeyAscii = 0
Else
If KeyAscii = 13 Then



End If
End If
Else

End If
leonkim 2001-11-27
  • 打赏
  • 举报
回复
Private Sub txtBom_KeyPress(KeyAscii As Integer)
If KeyAscii < 49 Or KeyAscii > 57 Then
txtbom.Locked = True
Else
If txtbom.Locked = True Then
txtbom.Locked = False
txtbom = txtbom & Chr(KeyAscii)
txtbom.SelStart = Len(txtbom.Text)
End If
End If
End Sub
这个一定没问题,我试过了。
IMHELLFIRE 2001-11-27
  • 打赏
  • 举报
回复
错了,应该是 48 to 57
不好意思
IMHELLFIRE 2001-11-27
  • 打赏
  • 举报
回复
在KeyPress事件里写
select case keyascii
case 47 to 57
case else
beep
keyascii=0
end select
leonkim 2001-11-27
  • 打赏
  • 举报
回复
Private Sub txtBom_KeyPress(KeyAscii As Integer)
If KeyAscii < 49 or KeyAscii > 57 Then
txtBom.locked=true
else
txtBom.locked=false
End If
End Sub
c7654321 2001-11-27
  • 打赏
  • 举报
回复
以下给出标准例程:
Private Sub Text1_KeyPress(Index As Integer, KeyAscii As Integer)
KeyAscii = FilterWhole(KeyAscii)
End Sub
Public Function FilterWhole(ByVal KeyAscii As Integer) As Integer
If (KeyAscii <> vbKeyDelete) And (KeyAscii <> vbKeyBack) _
And (KeyAscii < 48 Or KeyAscii > 57) Then
KeyAscii = 0
End If
FilterWhole = KeyAscii
End Function

lcooky 2001-11-27
  • 打赏
  • 举报
回复
对了是or 不是 and,呵呵
lhh 2001-11-27
  • 打赏
  • 举报
回复
谢谢了!
lcooky 2001-11-27
  • 打赏
  • 举报
回复
Private Sub txtBom_KeyPress(KeyAscii As Integer)
If KeyAscii < 49 And KeyAscii > 57 Then
txtBom.locked=true
else
txtBom.locked=false
End If
End Sub
snowboy1980 2001-11-27
  • 打赏
  • 举报
回复
If KeyAscii < 49 And KeyAscii > 57 Then
這條語句錯了﹐應該是﹕
If KeyAscii < 49 Or KeyAscii > 57 Then
ByTheWay 2001-11-27
  • 打赏
  • 举报
回复
看在MM的份上:)


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 = IntVal

End Function


7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧