WinForm中的TextBox只能输入数字的问题

_NET2004 2008-02-16 05:54:09
以下代码可以限制文本框只能输入数字,不能英文输入字符,但是也能输入中文字符,该怎么解决?


'取得用户输入的字符

Private Sub txtRs_Sy_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtRs_Sy.KeyPress
'数字0-9为48-57
Select Case Asc(e.KeyChar)
Case 48 To 57, 8, 13
e.Handled = False
Case Else
Beep()
e.Handled = True
End Select
...全文
694 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
rockyvan 2008-02-21
  • 打赏
  • 举报
回复
学习。
xinfeizhilian 2008-02-21
  • 打赏
  • 举报
回复
Private Function IsHalfNumberic(ByVal checkString As String) As Boolean

checkString = checkString.Replace(Chr(10), "").Replace(Chr(13), "")

If checkString Is Nothing OrElse checkString.Length = 0 Then
Return True
End If

If Not System.Text.RegularExpressions.Regex.IsMatch(checkString, "^-{0,1}[0-9]+\.{1}[0-9]+$|^-{0,1}[0-9]+$") Then
Return False
End If

Return True
End Function
_NET2004 2008-02-21
  • 打赏
  • 举报
回复
感谢楼上的各位朋友。
xmlNIIT 2008-02-21
  • 打赏
  • 举报
回复
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
'把TextBox的属性IMEMode的值改成Diable
If Char.IsDigit(e.KeyChar) Or e.KeyChar = Chr(8) Then
e.Handled = False
Else
e.Handled = True
End If


End Sub
Cauly123 2008-02-20
  • 打赏
  • 举报
回复
嘿嘿 Hook
whshuaish 2008-02-20
  • 打赏
  • 举报
回复
43720938 和 正则表达式 哪一个要好一点。??
zy227 2008-02-20
  • 打赏
  • 举报
回复
结帖阿
zy227 2008-02-18
  • 打赏
  • 举报
回复
根本不用判断 ! 因为只能输入数字了!
steven_srl 2008-02-18
  • 打赏
  • 举报
回复
输入好以后用Microsoft.VisualBasic.IsNumeric(Me.txtBox1.Text)判断就可以了,不是数字的话退回重新填写就可以了
zy227 2008-02-18
  • 打赏
  • 举报
回复
43720938 的方法很好 我以前就这么用过!!

支持!
flandy_feng 2008-02-17
  • 打赏
  • 举报
回复
Public Function keyCheck(ByVal e1 As KeyPressEventArgs) As Boolean
Dim keyascii As Integer = Asc(e1.KeyChar)
Select Case keyascii
Case Is < 32
Case 48 To 57
Case Else
keyascii = 0
End Select
If keyascii = 0 Then
e1.Handled = True
End If
End Function

这是一个键盘事件的方法啊
43720938 2008-02-17
  • 打赏
  • 举报
回复
Private Sub txtInput_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtInput.KeyPress
'保证输入为数字
If InStr("0123456789", e.KeyChar) = 0 And e.KeyChar <> Chr(27) Then
e.Handled = True
MsgBox("请输入数字!", MsgBoxStyle.Information, "提示")
Else
e.Handled = False
End If
End Sub
足球中国 2008-02-16
  • 打赏
  • 举报
回复
只能在KEYDOWN事件里判断因为那是判断的单个字符.是个ASCII字符.最准确
chenxu4277 2008-02-16
  • 打赏
  • 举报
回复
用TextChanged事件判断最好了,用keypress的话没办法避免用户用ctrl+v的方法输入英文

TextChanged事件中用正则表达式
_NET2004 2008-02-16
  • 打赏
  • 举报
回复
MASKTEXTBOX 在VS03里不能用吧
足球中国 2008-02-16
  • 打赏
  • 举报
回复
用MASKTEXTBOX
或者在KEYDOWN事件判断.
_NET2004 2008-02-16
  • 打赏
  • 举报
回复
汉字还是可以录入,没明白
新鲜鱼排 2008-02-16
  • 打赏
  • 举报
回复
其实你在意的是输入的结果,
Maybe you can try Control.KeyDown
http://msdn2.microsoft.com/zh-tw/library/system.windows.forms.control.keydown(VS.80).aspx

' Boolean flag used to determine when a character other than a number is entered.
Private nonNumberEntered As Boolean = False


' Handle the KeyDown event to determine the type of character entered into the control.
Private Sub textBox1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) _
Handles textBox1.KeyDown
' Initialize the flag to false.
nonNumberEntered = False

' Determine whether the keystroke is a number from the top of the keyboard.
If e.KeyCode < Keys.D0 OrElse e.KeyCode > Keys.D9 Then
' Determine whether the keystroke is a number from the keypad.
If e.KeyCode < Keys.NumPad0 OrElse e.KeyCode > Keys.NumPad9 Then
' Determine whether the keystroke is a backspace.
If e.KeyCode <> Keys.Back Then
' A non-numerical keystroke was pressed.
' Set the flag to true and evaluate in KeyPress event.
nonNumberEntered = True
End If
End If
End If
End Sub 'textBox1_KeyDown


' This event occurs after the KeyDown event and can be used
' to prevent characters from entering the control.
Private Sub textBox1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) _
Handles textBox1.KeyPress
' Check for the flag being set in the KeyDown event.
If nonNumberEntered = True Then
' Stop the character from being entered into the control since it is non-numerical.
e.Handled = True
End If
End Sub 'textBox1_KeyPress
End Class 'Form1

16,556

社区成员

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

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