关于TextBox中只允许输入数字的问题

foxegg 2005-02-27 03:42:49
我想在TextBox中只允许输入数字,有没有什么属性可以设置?还有,怎么屏蔽掉某些键盘输入。
...全文
259 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Night_Elf 2005-02-27
  • 打赏
  • 举报
回复
Private Sub txtMobieTel_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtMobieTel.KeyPress
Select Case e.KeyChar
Case "0" To "9"

Case " " '加入退格符
Case Else
e.Handled = True '使输入无效的语句
End Select
End Sub
hamadou 2005-02-27
  • 打赏
  • 举报
回复
Private Sub TextBox1_KeyPress(Byval sender As System.Object, Byval e As System.Windows.Forms.KeyPressEventArgs) Handles textBox1.KeyPress
If Char.IsNumber(e.KeyChar) Or e.KeyChar = Chr(Keys.Back) Then
Return
End If
e.Handled = True
End Sub
机器人 2005-02-27
  • 打赏
  • 举报
回复
方法1:屏蔽用户除了数字键以外的输入。前提ImeMode = Disable让用户无法切换中文或其他输入法。
Private Sub TextBox1_KeyPress(Byval sender As System.Object, Byval e As System.Windows.Forms.KeyPressEventArgs) Handles textBox1.KeyPress
If Char.IsNumber(e.KeyChar) Or e.KeyChar = Chr(Keys.Back) Then '退格应该允许。
Return
End If
e.Handled = True
End Sub
方法2:在TextBox的Validing事件用正则表达式验证。
Private Sub textBox1_Validating(ByVal sender As Object, _
ByVal e As System.ComponentModel.CancelEventArgs) Handles textBox1.Validating

Dim numericRegex As Regex = new Regex("^\d+$")

if Not numericRegex.Match(CType(sender,TextBox).Text) Then
' Cancel the event and select the text to be corrected by the user.
e.Cancel = True
textBox1.Select(0, textBox1.Text.Length)
End If
End Sub
yizhixiaozhu 2005-02-27
  • 打赏
  • 举报
回复
用js 或者用验证控件

16,555

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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