如何让text控件中只输入1、2、3、4、5、6、7、8、9、0这几个数字,其他的内容都不允许输入?

howdo123 2004-12-22 01:27:58
如何让text控件中只输入1、2、3、4、5、6、7、8、9、0这几个数字,其他的内容都不允许输入?
...全文
153 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
junki 2004-12-22
  • 打赏
  • 举报
回复
'根据KeyAscii范围
Private Sub Text1_GotFocus()
SelTextBox Text1
End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii > 58 Or KeyAscii < 47 Then
MsgBox "Error", vbOKOnly, vbExclamation
Call Text1_GotFocus
End If
End Sub

Sub SelTextBox(tb As TextBox)
tb.SelStart = 0 ' Set selection start.
tb.SelLength = Len(tb.Text) ' Set selection length.
End Sub
sysmaster 2004-12-22
  • 打赏
  • 举报
回复
使用like运算符是最省的方法
myhgyp 2004-12-22
  • 打赏
  • 举报
回复
同意:pcwak()
fxy_2002 2004-12-22
  • 打赏
  • 举报
回复

'text框的 keypress事代码
'在有需要的地方这样调用 presskey 就可以了
Private Sub text1_KeyPress(KeyAscii As Integer)
PressKey KeyAscii
End Sub

'按键处理
'如果需要输入小数点正负号,请改变常量 allowkey
private Sub PressKey(ByRef keyVal As Integer)
const allowKey As String="0123456789"
If InStr(allowKey, Chr(keyVal)) = 0 And keyVal <> 8 Then keyVal = 0
End Sub
pcwak 2004-12-22
  • 打赏
  • 举报
回复
Private Sub Text1_KeyPress(KeyAscii As Integer)
If (KeyAscii < 48 Or KeyAscii > 57) Then KeyAscii = 0
End Sub
tztz520 2004-12-22
  • 打赏
  • 举报
回复
Private Sub Text1_KeyPress(KeyAscii As Integer)
If IsNumeric(Chr(KeyAscii)) = False Then KeyAscii = 0
End Sub

1,453

社区成员

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

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