各位大侠,text内怎么只能输入数字?

stonea168 2003-10-18 09:38:41
各位大侠,text内怎么只能输入数字?谢谢
...全文
135 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
menuvb 2003-10-22
  • 打赏
  • 举报
回复
主 题: 屏蔽输入的字符
作 者: fenghuaxueyue (雪月)
信 誉 值: 95
所属论坛: .NET技术 VB.NET
问题点数: 10
回复次数: 1
发表时间: 2002-9-6 10:44:19




在VB。NET里我想在TEXTBOX 里只输入数字。把输入其他的字符屏蔽掉,请问怎么做!

帮帮忙!


回复人: Latitude(Henry) ( ) 信誉:98 2002-9-6 11:24:51 得分:10


Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

'8-BACKSPACE回删

'46-"."

'你可以补充写else作输入的处理

If Not (IsNumeric(e.KeyChar) Or e.KeyChar = Microsoft.VisualBasic.ChrW(8) Or e.KeyChar = Microsoft.VisualBasic.ChrW(46)) Then

e.Handled = True '表示已处理完,即不显示了

End If

End Sub



boxzang 2003-10-22
  • 打赏
  • 举报
回复
重载textbox的keypress事件,在里面输入以下代码
If Not Char.IsDigit(e.KeyChar) Then
e.Handled = True
Else
e.Handled = False
End If
stonea168 2003-10-21
  • 打赏
  • 举报
回复
thank you very much!!!!!!
Dugu_Niu 2003-10-21
  • 打赏
  • 举报
回复
接上文
做好之后编译生成dll文件,然后在你的项目中引用这个dll就可以
Dugu_Niu 2003-10-21
  • 打赏
  • 举报
回复
最好做一个用户控件,以便以后重复使用,
从TextBox继承
在KeyPress事件中写以下代码:
Dim KeyAscii As Integer
KeyAscii = Asc(e.KeyChar)

Select Case KeyAscii

Case 48 To 57, 8, 13 ' these are the digits 0-9, backspace,
' and carriage return
' we're OK on these, don't do anything

Case 45 ' minus sign

' if we already have one, throw it away
If InStr(Me.Text, "-") <> 0 Then
KeyAscii = 0
Exit Sub
End If

' if the insertion point is not sitting at zero
' (which is beginning of field), throw away the minus
' sign (because it's not valid except in first position)
If Me.SelectionStart <> 0 Then
KeyAscii = 0
End If

Case 46 ' this is a period (decimal point)

' if we already have a period, throw it away
If InStr(Me.Text, ".") <> 0 Then
KeyAscii = 0
End If

Case Else
' provide no handling for the other keys
KeyAscii = 0

End Select

' If we want to throw the keystroke away, then set the event
' as already handled. Otherwise, let the keystroke be handled normally.
If KeyAscii = 0 Then
e.Handled = True
Else
e.Handled = False
End If
NoReady 2003-10-21
  • 打赏
  • 举报
回复
在文本框的keyPress事件中:

If Asc(e.KeyChar) > 26 Then
If (Asc(e.KeyChar) < Keys.D0 Or Asc(e.KeyChar) > Keys.D9) Then
e.Handled = True
Else
e.Handled = False
End If
End If
citylamp 2003-10-19
  • 打赏
  • 举报
回复
不用这么麻烦,正则表达式就ok了
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
If Regex.IsMatch(TextBox3.Text, "^[\d|+|-]\d+[.]\d$") Then
MsgBox("OK!!")
Else
TextBox3.Text = ""
End If
End Sub
block 2003-10-18
  • 打赏
  • 举报
回复
单纯数字好办,问题是有些工程上记数就比较麻烦了,比如1.2e-12.5
SqlDataAdapter 2003-10-18
  • 打赏
  • 举报
回复
最简单的方法:
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If Not (IsNumeric(e.KeyChar) Or e.KeyChar = Microsoft.VisualBasic.ChrW(8) Or e.KeyChar = Microsoft.VisualBasic.ChrW(46)) Then
e.Handled = True
End If
End Sub
stonea168 2003-10-18
  • 打赏
  • 举报
回复
我是这样做的
For i = 0 To TextBox1.Text.Length
TextBox1.SelectionStart = i
TextBox1.SelectionLength = i + 1

If TextBox1.Text = Nothing Then
MessageBox.Show("请输入数字", "warning")
Return
End If


j = Asc(TextBox1.Text)

If j < 48 Or j > 57 Then
MessageBox.Show("请输入数字", "warning")
TextBox1.Clear()
Return
End If

Next

但是输入前边是数字后边是字符的话 就会出错,,
之样做是不是很笨
有没有更好的解决办法,

16,553

社区成员

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

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